package com.teamdev.jxbrowser.chromium.demo.k_spider.util;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public
class HttpClient_Get_Url {
/**
* 根据百度url,获取原本url
* @throws IOException
* @throws HttpException
* */
public static String GetTrueUrlByBaiduUrl(String baidu_url) throws HttpException, IOException{
//---------------------------1
HttpClient client = new HttpClient();
//设置代理IP
//client.getHostConfiguration().setProxy("172.22.40.20", 8080);
GetMethod getMethod = new GetMethod(baidu_url);
//获取状态码
int stateCode =client.executeMethod(getMethod);
String text=getMethod.getResponseBodyAsString();
//释放
getMethod.releaseConnection();
if (stateCode == HttpStatus.SC_OK) {
text=text.split("URL='")[1].split("'")[0];
//System.out.println("访问成功,网址:"+text);
return text;
}
return null;
}
/**
* 根据百度url,获取stateCode
* @throws IOException
* @throws HttpException
* */
public static int GetStateCode(String url) throws HttpException, IOException{
//---------------------------1
HttpClient client = new HttpClient();
//设置代理IP
//client.getHostConfiguration().setProxy("172.22.40.20", 8080);
GetMethod getMethod = new GetMethod(url);
//获取状态码
int stateCode =client.executeMethod(getMethod);
return stateCode;
}
/**
* httpclient请求url返回html
* @throws IOException
* @throws ClientProtocolException
* */
public static String getHtmlByHttpclient(String url) throws ClientProtocolException, IOException{
//httpclient访问
CloseableHttpClient httpclient = HttpClients.create
Default();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
String ahtml = EntityUtils.toString(entity, "UTF-8");
httpget.releaseConnection();//释放连接
//System.out.println(ahtml);
return ahtml;
}
public static void main(String[] args) throws HttpException, IOException {
//String url="https://mainsite-restapi.ele.me/shopping/restaurant/150781362?extras%5B%5D=activities&extras%5B%5D=license&extras%5B%5D=identification&extras%5B%5D=albums&extras%5B%5D=flavors&latitude=29.533694&longitude=106.486673";
String url="https://www.baidu.com/link?url=iOgfQtomBGqwU0cQTGeK3V3U7QPh9i96fEhSU0INNhe&wd=&eqid=9b65b1c00000dc0b000000035b42cc0e";
System.out.println(GetTrueUrlByBaiduUrl(url));
}
}