If the above doesn't apply, you may try to use the following code (hack) to skip the re-direction,
get the Location header and re-connect:
try {
URL url = new URL("http://www.ama.org/events ");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setFollowRedirects(true);
con.setInstanceFollowRedirects(false);
con.connect();
while (String.valueOf(con.getResponseCode()).startsWith("3")) {
String theLocation = con.getHeaderField("Location");
con.disconnect();
url = new URL(theLocation);
con = (HttpURLConnection) url.openConnection();
con.setFollowRedirects(true);
con.setInstanceFollowRedirects(false);
con.connect();
}
/** at this point you are located at the last(target)page of
redirection chain */
} catch (Exception ex) { ex.printStackTrace(); }
分享到:
相关推荐
JAVAHttpClientThe server failed to respond with a validCaused by: org.apache.htt
jsoup开发java的html爬虫 统计词频 归纳搜索结果 jsoup是个好东西
com.android.volley.NoConnectionError: java.io.InterruptedIOException”,内容加载失败,问题出在重复调用 queue.start() 方法。 错误提示:com.android.volley.NoConnectionError: java.io....
org.apache.http.ProtocolException.class org.apache.http.ProtocolVersion.class org.apache.http.ReasonPhraseCatalog.class org.apache.http.RequestLine.class org.apache.http.StatusLine.class org.apache....
Java提供的网络开发辅助类都包含在java.net包中,其主要的类和可能产生的异常包括: 面向IP层的类:InetAddress 面向应用层的类:URL、URLConnection TCP协议相关类:Socket、ServerSocket UDP协议相关类:...