02_Httpclient
Posted HigginCui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了02_Httpclient相关的知识,希望对你有一定的参考价值。
【实例1. GET请求百度(乱码)】
/** * Http GET请求百度,但是返回乱码 */ public static void main(String[] args) throws Exception { //创建一个默认的HttpClient类 CloseableHttpClient httpclient=HttpClients.createDefault(); //设置Http连接方式为get,访问的URL为"http://www.baidu.com" HttpGet httpGet=new HttpGet("http://www.baidu.com"); //得到返回的response对象 HttpResponse response=httpclient.execute(httpGet); //得到response里面的实体信息 HttpEntity entity=response.getEntity(); //利用EntityUtils工具类将实体信息转换为字符串 String html=EntityUtils.toString(entity); //打印 System.out.println(html); //关闭链接 httpclient.close(); }
【实例2.Get请求百度,解决乱码问题】
public static void main(String[] args) throws Exception { //创建一个默认的HttpClient类 CloseableHttpClient httpclient=HttpClients.createDefault(); //设置Http连接方式为get,访问的URL为"http://www.baidu.com" HttpGet httpGet=new HttpGet("http://www.baidu.com"); //得到返回的response对象 HttpResponse response=httpclient.execute(httpGet); //得到response里面的实体信息 HttpEntity entity=response.getEntity(); //利用EntityUtils工具类将实体信息转换为字符串 String html=EntityUtils.toString(entity,"UTF-8"); //打印 System.out.println(html); //关闭链接 httpclient.close(); }
【实例3.带各种消息头的GET请求】
@Test public void testRequestHeader() throws ClientProtocolException, IOException{ CloseableHttpClient httpclient=HttpClients.createDefault(); HttpGet httpGet=new HttpGet("http://download.csdn.net/my/uploads"); //访问CSDN的"下载资源"页面 //带上cookie请求消息头 httpGet.setHeader("cookie", "uuid_tt_dd=439312343077018427_20160728; bdshare_firstime=2345017309443; _JQCMT_ifcookie=1; ********************"); //登录之后用浏览器获取对应给的Cookie httpGet.setHeader("Host","download.csdn.net"); httpGet.setHeader("Referer","http://download.csdn.net/my/uploads"); HttpResponse response=httpclient.execute(httpGet); HttpEntity entity=response.getEntity(); String html=EntityUtils.toString(entity,"UTF-8"); System.out.println(html); httpclient.close(); }
【实例4. 获取Get请求百度后 响应页面的各个信息(状态行、消息头、响应正文)】
@Test public void test01() throws Exception{ CloseableHttpClient httpclient=HttpClients.createDefault(); HttpGet httpGet=new HttpGet("http://www.baidu.com"); HttpResponse response=httpclient.execute(httpGet); //响应的response对象 System.out.println("【 响应对象 】 \\n"+response); //响应的状态行 System.out.println("【 响应的状态行 】 \\n"+response.getStatusLine()); //获得响应状态行的状态码 System.out.println("【 响应的状态行的状态码 】 \\n"+response.getStatusLine().getStatusCode()); //响应实体 System.out.println("【 响应实体 】 \\n"+response.getEntity()); //遍历响应所有的消息头 System.out.println("=====================【 遍历打印所有的消息头】======================"); Header[] headers=response.getAllHeaders(); for(Header header:headers){ System.out.println(header.getName()+"======="+header.getValue()); } //获取特定的响应头 System.out.println("=====================【 获取特定响应头(Server) 】======================\\n"+response.getHeaders("Server")[0].getName()+"-----------------"+response.getHeaders("Server")[0].getValue()); //将响应的页面转化成字符串 System.out.println("=====================【 将响应的页面转化成字符串 】========================="); HttpEntity entity=response.getEntity(); String html=EntityUtils.toString(entity,"UTF-8"); System.out.println(html); //关闭连接 httpclient.close(); }
[ 运行结果 ]
【实例5.利用URIBuilder创建带参数的URI的方式一(常用)】
/** * 利用URIBuilder创建带参数的URI * 例子:搜狗查询 关键字搜索"HigginCui" */ @Test public void testURI() throws Exception{ CloseableHttpClient httpclient=HttpClients.createDefault(); URIBuilder uriBuilder=new URIBuilder("http://www.sogou.com/web"); uriBuilder.setParameter("query", "HigginCui"); //其实这里也可以用uriBuilder.addParameter("query","HigginCui")来代替,两者有一点区别,具体看源码 uriBuilder.setParameter("ie", "utf-8"); URI uri=uriBuilder.build(); System.out.println("uri=="+uri); //uri==http://www.sogou.com/web?query=HigginCui&ie=utf-8 HttpGet httpGet=new HttpGet(uri); HttpResponse response=httpclient.execute(httpGet); //将响应的页面转化成字符串 System.out.println("=====================【 将响应的页面转化成字符串 】========================="); HttpEntity entity=response.getEntity(); String html=EntityUtils.toString(entity,"UTF-8"); System.out.println(html); httpclient.close(); }
[ 浏览器用这个URI去进行搜索的实际效果 ]
【实例6.利用URIBuilder创建带参数的URI的方式二】
/** * 利用URIBuilder创建带参数的URI的第二种方式 * 例子:搜狗查询 关键字搜索"HigginCui" */ @Test public void testURI2() throws Exception{ CloseableHttpClient httpclient=HttpClients.createDefault(); URIBuilder uriBuilder=new URIBuilder("http://www.sogou.com/web"); List<NameValuePair> valuePairs=new ArrayList<NameValuePair>(); valuePairs.add(new BasicNameValuePair("query", "HigginCui")); valuePairs.add(new BasicNameValuePair("ie", "utf-8")); uriBuilder.addParameters(valuePairs); URI uri=uriBuilder.build(); System.out.println("uri=="+uri); //uri==http://www.sogou.com/web?query=HigginCui&ie=utf-8 HttpGet httpGet=new HttpGet(uri); HttpResponse response=httpclient.execute(httpGet); //将响应的页面转化成字符串 System.out.println("=====================【 将响应的页面转化成字符串 】========================="); HttpEntity entity=response.getEntity(); String html=EntityUtils.toString(entity,"UTF-8"); System.out.println(html); httpclient.close(); }
【实例7.关于RequestConfig类(HTTPRequest的配置信息)】
Test public void testRequestConfig() throws ClientProtocolException, IOException{ CloseableHttpClient httpclient=HttpClients.createDefault(); HttpGet httpGet=new HttpGet("http://www.baidu.com"); /**设置请求和传输的超时时间**/ RequestConfig requestConfig=RequestConfig.custom() .setConnectionRequestTimeout(50) //设置从连接池获取连接的超时时间 .setConnectTimeout(50) //设置连接远端服务器的超时时间 .setSocketTimeout(50) //设置从远端服务器上传输数据回来的超时时间 .build(); httpGet.setConfig(requestConfig); //将配置requestConfig设置到HttpGet请求中 CloseableHttpResponse response=httpclient.execute(httpGet); response.close(); httpclient.close(); }
【实例8.设置代理,利用Fiddler捕获本地的请求】
@Test public void testRequestConfigGet() throws ClientProtocolException, IOException{ CloseableHttpClient httpclient=HttpClients.createDefault(); HttpGet httpGet=new HttpGet("http://www.baidu.com"); /**设置请求和传输的超时时间**/ RequestConfig.Builder builder=RequestConfig.custom() .setConnectionRequestTimeout(500) .setConnectTimeout(500) .setSocketTimeout(500); String proxy="127.0.0.1:8888"; //本地Fiddler代理监听的端口 builder.setProxy(HttpHost.create(proxy)); //设置代理 RequestConfig requestConfig=builder.build(); httpGet.setConfig(requestConfig); //将配置requestConfig设置到HttpGet请求中 //带上cookie请求消息头 httpGet.setHeader("Cookie", "***************HigginCui***************"); httpGet.setHeader("Accept","*/*"); CloseableHttpResponse response=httpclient.execute(httpGet); response.close(); httpclient.close(); }
[ 运行之后的fiddler捕获的内容 ]
以上是关于02_Httpclient的主要内容,如果未能解决你的问题,请参考以下文章