|
|
@@ -112,13 +112,7 @@ public class HttpUtils {
|
|
|
}
|
|
|
log.info("sendGet - {}", urlNameString);
|
|
|
try {
|
|
|
- urlNameString = getRedirectUrl(urlNameString);
|
|
|
- URL realUrl = new URL(urlNameString);
|
|
|
- URLConnection connection = realUrl.openConnection();
|
|
|
- connection.setRequestProperty("accept", "*/*");
|
|
|
- connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
- connection.setRequestProperty("user-agent",
|
|
|
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
+ URLConnection connection = getRedirectUrlConnection(urlNameString);
|
|
|
connection.connect();
|
|
|
return connection.getInputStream();
|
|
|
} catch (ConnectException e) {
|
|
|
@@ -260,8 +254,7 @@ public class HttpUtils {
|
|
|
* @throws Exception 读写异常
|
|
|
*/
|
|
|
private static String getRedirectUrl(String path) throws Exception {
|
|
|
- HttpURLConnection conn = (HttpURLConnection) new URL(path)
|
|
|
- .openConnection();
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
|
|
|
conn.setInstanceFollowRedirects(false);
|
|
|
conn.setConnectTimeout(5000);
|
|
|
String redirectUrl = conn.getHeaderField("Location");
|
|
|
@@ -271,6 +264,26 @@ public class HttpUtils {
|
|
|
return redirectUrl;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取重定向地址
|
|
|
+ * @param path 原URL地址
|
|
|
+ * @return 重定向之后的地址
|
|
|
+ * @throws Exception 读写异常
|
|
|
+ */
|
|
|
+ private static URLConnection getRedirectUrlConnection(String path) throws Exception {
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
|
|
|
+ conn.setInstanceFollowRedirects(false);
|
|
|
+ conn.setConnectTimeout(5000);
|
|
|
+ String redirectUrl = conn.getHeaderField("Location");
|
|
|
+ if(null != redirectUrl){
|
|
|
+ URL realUrl = new URL(redirectUrl);
|
|
|
+ conn = (HttpURLConnection) realUrl.openConnection();
|
|
|
+ }
|
|
|
+ conn.setRequestProperty("accept", "*/*");
|
|
|
+ conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
+ return conn;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 将输入流中的数据写入字节数组
|