httprequest—java程序如何在303HTTP状态码响应中自动遵循重定向

pkbketx9  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(196)

这个问题在这里已经有答案了

java-如何找到url的重定向url(6个答案)
5年前关门了。
我试图在我的java程序中访问这个url,但是我得到了这个奇怪的消息,而不是我所期望的页面内容。
我怎样才能避免这种情况?

<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
 <head> 
  <title>303 See Other</title> 
 </head>
 <body> 
  <h1>See Other</h1> 
  <p>The answer to your request is located <a href="https://www.wikidata.org/wiki/Special:EntityData/P26">here</a>.</p>  
 </body>
</html>

在浏览器中,虽然我可以很容易地导航到那里。是否有一些函数或库可以用来从java程序中调用这些功能?

for (String url : list_of_relation_URLs) 
{
    //System.out.println( url );

    //go to relation url
    String URL_czech = url;

    System.out.println( url  );

    URL wikidata_page = new URL(URL_czech);
    HttpURLConnection wiki_connection = (HttpURLConnection)wikidata_page.openConnection();
    InputStream wikiInputStream = null;

    try 
    {
        // try to connect and use the input stream
        wiki_connection.connect();
        wikiInputStream = wiki_connection.getInputStream();
    } 
    catch(IOException error) 
    {
        // failed, try using the error stream
        wikiInputStream = wiki_connection.getErrorStream();
    }

    // parse the input stream using Jsoup
    Document docx = Jsoup.parse(wikiInputStream, null, wikidata_page.getProtocol()+"://"+wikidata_page.getHost()+"/");

    System.out.println( docx.toString() );  
}

我想做的基本上和这里发生的事情相反。

j8yoct9x

j8yoct9x1#

当您收到303状态码时,您只需向303提供的url发出第二个请求。
新的url存储在 Location 标题。
在您的情况下,您将需要继续跟踪,直到您得到一个不同的状态码,因为您将被重定向两次。
303:位置:“https://www.wikidata.org/wiki/special:实体数据/p26“
303:位置:“https://www.wikidata.org/wiki/property:第26页“
是的。。。如果您使用的是 HttpURLConnection 你可以让它帮你做这个。

conn.setInstanceFollowRedirects(true);
z4bn682m

z4bn682m2#

这是完美的答案

try {

String url = "http://www.twitter.com";

URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");

System.out.println("Request URL ... " + url);

boolean redirect = false;

// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
    if (status == HttpURLConnection.HTTP_MOVED_TEMP
        || status == HttpURLConnection.HTTP_MOVED_PERM
            || status == HttpURLConnection.HTTP_SEE_OTHER)
    redirect = true;
}

System.out.println("Response Code ... " + status);

if (redirect) {

    // get redirect url from "location" header field
    String newUrl = conn.getHeaderField("Location");

    // get the cookie if need, for login
    String cookies = conn.getHeaderField("Set-Cookie");

    // open the new connnection again
    conn = (HttpURLConnection) new URL(newUrl).openConnection();
    conn.setRequestProperty("Cookie", cookies);
    conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
    conn.addRequestProperty("User-Agent", "Mozilla");
    conn.addRequestProperty("Referer", "google.com");

    System.out.println("Redirect to URL : " + newUrl);

}

BufferedReader in = new BufferedReader(
                          new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer html = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    html.append(inputLine);
}
in.close();

System.out.println("URL Content... \n" + html.toString());
System.out.println("Done");

相关问题