Httpclient

什么是httpclient

HttpClient是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)

下载地址:http://hc.apache.org/


httpclient作用

在java代码中,发送Http请求。通常用来实现远程接口调用。

HttpClient测试

说明:在ego-base中测试

在ego-base工程中添加httpclient的pom依赖。

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

</dependency>


执行GET请求

public static void doGet(){

 //创建Httpclient对象

        CloseableHttpClient httpclient = HttpClients.createDefault();


        //创建httpGET请求

        HttpGet httpGet = new HttpGet("http://www.oschina.net/");


        CloseableHttpResponse response = null;

        try {

            //执行请求

            response = httpclient.execute(httpGet);

            System.out.println(response.getStatusLine());

            //判断返回状态是否为200

            if (response.getStatusLine().getStatusCode() == 200) {

                String content = EntityUtils.toString(response.getEntity(), "UTF-8");

                System.out.println("内容长度:" + content.length());

            }

        }catch(Exception e){

         e.printStackTrace();


        } finally {

            if (response != null) {

                try {

response.close();

} catch (IOException e) {

e.printStackTrace();

}

            }

            try {

httpclient.close();

} catch (IOException e) {

e.printStackTrace();

}

        }


执行GET带参数

public static void doGetParam(){

 //创建Httpclient对象

CloseableHttpClient httpclient = HttpClients.createDefault();

CloseableHttpResponse response = null;

try {


        //定义请求的参数

        URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "数据库").build();


        System.out.println(uri);


        //创建http GET请求

        HttpGet httpGet = new HttpGet(uri);


            //执行请求

            response = httpclient.execute(httpGet);

            //判断返回状态是否为200

            if (response.getStatusLine().getStatusCode() == 200) {

                String content = EntityUtils.toString(response.getEntity(), "UTF-8");

                System.out.println(content);

            }

        }catch(Exception e){

         e.printStackTrace();


        }finally {

            if (response != null) {

                try {

response.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

            }

            try {

httpclient.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        }

}


执行post请求

public static void doPost(){

 //创建Httpclient对象

        CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();


        //创建httpPOST请求

        HttpPost httpPost = new HttpPost("http://www.oschina.net/");


        CloseableHttpResponse response = null;

        try {

            //执行请求

            response = httpclient.execute(httpPost);

            System.out.println(response.getStatusLine());

            //判断返回状态是否为200

            if (response.getStatusLine().getStatusCode() == 200) {

                String content = EntityUtils.toString(response.getEntity(), "UTF-8");

                System.out.println(content);

            }

        }catch(Exception e){

         e.printStackTrace();


        }finally {

            if (response != null) {

                try {

response.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

            }

            try {

httpclient.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        }

    }


执行post带参数

public static void doPostParam() throws Exception{

 //创建Httpclient对象

        CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();


        //创建httpPOST请求

        HttpPost httpPost = new HttpPost("http://www.oschina.net/search");


        //设置2个post参数,一个是scope、一个是q

        List<NameValuePair> parameters = new ArrayList<NameValuePair>();

        parameters.add(new BasicNameValuePair("scope", "project"));

        parameters.add(new BasicNameValuePair("q", "java"));

        //构造一个form表单式的实体

        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);

        //将请求实体设置到httpPost对象中

        httpPost.setEntity(formEntity);


        CloseableHttpResponse response = null;

        try {

            //执行请求

            response = httpclient.execute(httpPost);

            System.out.println(response.getStatusLine());

            //判断返回状态是否为200

            if (response.getStatusLine().getStatusCode() == 200) {

                String content = EntityUtils.toString(response.getEntity(), "UTF-8");

                System.out.println(content);

            }

        } finally {

            if (response != null) {

                response.close();

            }

            httpclient.close();

        }

}


httpclient常见问题及解决方案

请求参数乱码

设置请求的编码格式:

obj.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");


响应HTTP/1.1 403 Forbidden

原因:网站设置了反爬虫机制,禁止非法访问。

解决方案:伪装浏览器。

obj.addHeader("User-Agent"," Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537. 36 (KHTML, like Gecko) Chrome/31.0.1650.63")



封装通用工具类HttpClientUtils

public class HttpClientUtils {


public static String doGet(String url,Map<String, String> params){

//获取httpclient客户端

CloseableHttpClient httpclient = HttpClients.createDefault();

String resultString = "";

CloseableHttpResponse response = null;

try {

URIBuilder builder = new URIBuilder(url);

if(null!=params){

for (String key:params.keySet()) {

builder.setParameter(key, params.get(key));

}

}

HttpGet get = new HttpGet(builder.build());

response = httpclient.execute(get);

System.out.println(response.getStatusLine());

if(200==response.getStatusLine().getStatusCode()){

HttpEntity entity = response.getEntity();

resultString = EntityUtils.toString(entity, "utf-8");

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if(null!=response){

try {

response.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(null!=httpclient){

try {

httpclient.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return resultString;

}

public static String doGet(String url){

return doGet(url, null);

}

public static String doPost(String url,Map<String,String> params){

/**

*在4.0及以上httpclient版本中,post需要指定重定向的策略,如果不指定则按默认的重定向策略。

 *

*获取httpclient客户端

 */

CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy( new LaxRedirectStrategy()).build();

String resultString = "";

CloseableHttpResponse response = null;

try {

HttpPost post = new HttpPost(url);

List<NameValuePair> paramaters = new ArrayList<>();

if(null!=params){

for (String key : params.keySet()) {

paramaters.add(new BasicNameValuePair(key,params.get(key)));

}

UrlEncodedFormEntity  formEntity = new UrlEncodedFormEntity (paramaters);

post.setEntity(formEntity);

}

/**

 * HTTP/1.1 403 Forbidden

*原因:

*有些网站,设置了反爬虫机制

*解决的办法:

*设置请求头,伪装浏览器

 */

post.addHeader("user-agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");

response = httpclient.execute(post);

System.out.println(response.getStatusLine());

if(200==response.getStatusLine().getStatusCode()){

HttpEntity entity = response.getEntity();

resultString = EntityUtils.toString(entity, "utf-8");

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if(null!=response){

try {

response.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(null!=httpclient){

try {

httpclient.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return resultString;

}

public static String doPost(String url){

return doPost(url, null);

}

 }

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容