HttpClient

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpClient相关的知识,希望对你有一定的参考价值。

 导入依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

HttpClient API使用

技术分享
package cn.cslg.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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 HttpClinet {

    public static void main(String[] args) {
        CloseableHttpClient httpClient=HttpClients.createDefault(); // 创建httpClient实例
        HttpGet httpGet=new HttpGet("http://www.tuicool.com"); // 创建httpget实例
        HttpHost proxy=new HttpHost("220.166.96.90", 82); // 创建代理主机
        RequestConfig config=RequestConfig.custom()
                .setConnectTimeout(10000) // 设置连接超时时间10秒钟
                .setSocketTimeout(10000) // 设置读取超时时间10秒钟
                .setProxy(proxy) // 设置代理
                .build();
        httpGet.setConfig(config); 
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"); // 设置头信息,模拟浏览器访问
        CloseableHttpResponse response=null;
        try {
            response=httpClient.execute(httpGet); //执行http get请求
        } catch (ClientProtocolException e) { // http协议异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) { // io异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpEntity entity=response.getEntity(); // 获取返回实体
        try {
            System.out.println("Status:"+response.getStatusLine().getStatusCode()); // 获取响应状态码
            System.out.println("Content-Type:"+entity.getContentType().getValue()); // 获取响应头类型
            System.out.println("网页内容:"+EntityUtils.toString(entity, "utf-8")); // 获取网页内容
        } catch (ParseException e) { // 解析异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) { // io异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            response.close(); // response关闭
        } catch (IOException e) { // io异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            httpClient.close(); // httpClient关闭
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
View Code

 

以上是关于HttpClient的主要内容,如果未能解决你的问题,请参考以下文章

WP8,怎么使用HttpClient?

android的自带的httpClient 怎么上传文件

怎么用http协议实现安卓数据

HttpClient配置及示例代码

HttpClient学习--HttpClient的POST请求过程源码解读

java用httpclient 执行到httpclient.execute(request); 这句代码假死不往下执行不报错