java根据百度url获取真正的网页地址_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java根据百度url获取真正的网页地址

java根据百度url获取真正的网页地址

 2018/7/9 12:20:38  st4024589553  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.teamdev.jxbrowser.chromium.demo.k_spider.util;importjava.io.IOException;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpException;importorg.apache.commons.httpclient.HttpStatus;importorg.apache
  • 标签:Java URL 真正 百度 网页
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.createDefault();
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));
}
}
发表评论
用户名: 匿名