首页 > 代码库 > 02_Httpclient

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请求】

@Testpublic 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请求百度后 响应页面的各个信息(状态行、消息头、响应正文)】

@Testpublic 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();}

[ 运行结果 ]

技术分享

 

02_Httpclient