package org.fanli.http;
|
|
import java.net.URLDecoder;
|
|
import org.apache.commons.httpclient.HttpClient;
|
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
import org.apache.commons.httpclient.methods.GetMethod;
|
import org.apache.commons.httpclient.params.HttpMethodParams;
|
|
public class GetUrl {
|
|
public static void main(String[] args) {
|
String urlStr = "https://s.click.taobao.com/t?e=m%3D2%26s%3DT9DFpHQCT7Rw4vFB6t2Z2ueEDrYVVa64XoO8tOebS+dRAdhuF14FMR8PbBytQEje1aH1Hk3GeOiJjPfjWNBTvGChS8XTLgqFsyp3PHGafE9hz7ZCEfUCBSQYRxDRWsrMAV6zoU7+jzw6Wck6eaoQ6c0Q9fK1X0AuhLjO8JFSuq5qGmoLahT+Zz+Fa8QW0pMbTc7jC1Dcr3S4/ApvI96NZkEtjKTgb9sQVVYTniU5e5UcTNWtqRPezzPihM1enhJMVFzNXDvVTOYkiM0JmU9fSqK5FrSzWxEyPXsNY/sDjpw%3D&union_lens=lensId:0b0130fe_0bf6_16d5df25895_5125&xId=5txrpEx2YVvmhganW7raNMxAtFzGZyc3E72r5XE9ewJTxYx9HdVExRGtktqdlYc3fJWaixqcyyV0dBfQb8MgXT&union_lens=lensId:0b08107f_0c5e_16d5df25896_b064&utm_campaign=client_share&app=aweme&utm_medium=ios&tt_from=copy&utm_source=copy";
|
HttpClient client;
|
client = new HttpClient(new MultiThreadedHttpConnectionManager());
|
client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
|
client.getParams().setParameter(HttpMethodParams.USER_AGENT,
|
"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)"); // 让服务器认为是IE
|
|
GetMethod get = new GetMethod(urlStr);
|
|
get.setFollowRedirects(false); // 禁止自动重定向
|
|
try {
|
int iGetResultCode = client.executeMethod(get);
|
String location = get.getResponseHeader("location").getValue(); // 打印地址
|
GetMethod get2 = new GetMethod(URLDecoder.decode(location.split("tu=")[1]));
|
get2.addRequestHeader("Referer", location);
|
get2.setFollowRedirects(false); // 禁止自动重定向
|
int iGetResultCode2 = client.executeMethod(get2);
|
String realUrl = get2.getResponseHeader("location").getValue(); // 打印地址
|
System.out.println("REAL URL: " + realUrl);
|
GetMethod get3 = new GetMethod(realUrl);
|
int iGetResultCode3 = client.executeMethod(get3);
|
String responseBody = get3.getResponseBodyAsString();
|
// System.out.println("Response Body: "+responseBody);
|
System.out.println(responseBody.substring(responseBody.indexOf("sellerRate=" + "") + 12,
|
responseBody.indexOf("&", responseBody.indexOf("sellerRate="))));
|
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
} finally {
|
get.releaseConnection();
|
}
|
}
|
|
}
|