Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java/HttpURLConnection检索特定URL的重定向URL_Java_Httpurlconnection_Http Redirect_Url Redirection - Fatal编程技术网

使用Java/HttpURLConnection检索特定URL的重定向URL

使用Java/HttpURLConnection检索特定URL的重定向URL,java,httpurlconnection,http-redirect,url-redirection,Java,Httpurlconnection,Http Redirect,Url Redirection,如何获取以下URL的“最终位置”(也称为登录页): 我的代码(如下)将此字符串作为输入 输出应该类似于“”,但我只得到相同的URL 不用说,我无法解决这个具体案例,但我仍然需要一个通用的解决方案 下面是我的简单Java代码,使用HttpURLConnection(其中String ref是输入): 有人知道我做错了什么吗?服务器返回以下信息: <html> <head> <meta http-equiv="refresh" content="1; url=http

如何获取以下URL的“最终位置”(也称为登录页):

我的代码(如下)将此字符串作为输入

输出应该类似于“”,但我只得到相同的URL

不用说,我无法解决这个具体案例,但我仍然需要一个通用的解决方案

下面是我的简单Java代码,使用HttpURLConnection(其中String ref是输入):


有人知道我做错了什么吗?

服务器返回以下信息:

<html>
<head>
<meta http-equiv="refresh" content="1; url=http://weeklyad.target.com">
<title>Redirect</title>
<script language="javascript" type="text/javascript">
<!--
function track_click(url)
{
    var req = new Image();
    req.src = url;
}

function redirect(url)
{
    window.location = url;
}

var url_raw = "http://weeklyad.target.com";
var url_enc = "http%3A%2F%2Fweeklyad.target.com";

track_click("http://track.pubmatic.com/AdServer/AdDisplayTrackerServlet?clickData=JnB1YklkPTIwOTc3JnNpdGVJZD0zMDE1MSZhZElkPTI2NjA0JmthZHNpemVpZD05JnRsZElkPTAmcGFzc2JhY2s9MCZjYW1wYWlnbklkPTEyMTYmY3JlYXRpdmVJZD0wJmFkU2VydmVySWQ9MjQz_url=" + url_enc);

var redirect_timeout = 300;
setTimeout('redirect("http://weeklyad.target.com")', redirect_timeout);
// -->
</script></head><body></body></html>

重新使用
因此,发生重定向是因为调用了
重定向
函数(javascript),而不是
位置
(头)重定向


顺便说一句:通过查看原始URL,您可以看到您将到达的位置,请注意
&重定向=http://weeklyad.target.com
参数

关于“顺便说一句”备注:在这个特定示例中可能是这样,但在许多其他示例中不是这样;正如我提到的,我需要一个通用的解决方案;当然,我只需要登录页面的URL,而不是整个页面的内容;谢谢你除非你完全模拟一个浏览器,而不仅仅是执行一个HTTP GETSo你是说我应该使用Selenium或类似的东西吗?是的,你在这里需要完全的浏览器模拟,或者处理位置和JS重定向,但你仍然可能错过元刷新,所以你也需要涵盖这一点,因此,最好的选择是硒。根据研究,即使硒也可能无法做到这一点。
<html>
<head>
<meta http-equiv="refresh" content="1; url=http://weeklyad.target.com">
<title>Redirect</title>
<script language="javascript" type="text/javascript">
<!--
function track_click(url)
{
    var req = new Image();
    req.src = url;
}

function redirect(url)
{
    window.location = url;
}

var url_raw = "http://weeklyad.target.com";
var url_enc = "http%3A%2F%2Fweeklyad.target.com";

track_click("http://track.pubmatic.com/AdServer/AdDisplayTrackerServlet?clickData=JnB1YklkPTIwOTc3JnNpdGVJZD0zMDE1MSZhZElkPTI2NjA0JmthZHNpemVpZD05JnRsZElkPTAmcGFzc2JhY2s9MCZjYW1wYWlnbklkPTEyMTYmY3JlYXRpdmVJZD0wJmFkU2VydmVySWQ9MjQz_url=" + url_enc);

var redirect_timeout = 300;
setTimeout('redirect("http://weeklyad.target.com")', redirect_timeout);
// -->
</script></head><body></body></html>