分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

HttpClient 4.5.3( get和post请求)

发布时间:2023-09-06 02:33责任编辑:胡小海关键词:暂无标签

版本HttpCilent 4.5.3 

1.GET请求

CloseableHttpClient httpCilent = HttpClients.createDefault();//Creates CloseableHttpClient instance with default configuration.HttpGet httpGet = new HttpGet("http://www.baidu.com");try { ???httpCilent.execute(httpGet);} catch (IOException e) { ???e.printStackTrace();}finally { ???try { ???????httpCilent.close();//释放资源 ???} catch (IOException e) { ???????e.printStackTrace(); ???}}

2.GET请求,设置 超时时间

CloseableHttpClient httpCilent2 = HttpClients.createDefault(); ???????RequestConfig requestConfig = RequestConfig.custom() ???????????????.setConnectTimeout(5000) ??//设置连接超时时间 ???????????????.setConnectionRequestTimeout(5000) // 设置请求超时时间 ???????????????.setSocketTimeout(5000) ???????????????.setRedirectsEnabled(true)//默认允许自动重定向 ???????????????.build(); ???????HttpGet httpGet2 = new HttpGet("http://www.baidu.com"); ???????httpGet2.setConfig(requestConfig); ???????String srtResult = ""; ???????try { ???????????HttpResponse httpResponse = httpCilent2.execute(httpGet2); ???????????if(httpResponse.getStatusLine().getStatusCode() == 200){ ???????????????srtResult = EntityUtils.toString(httpResponse.getEntity());//获得返回的结果 ???????????????System.out.println(srtResult); ???????????}else if(httpResponse.getStatusLine().getStatusCode() == 400){ ???????????????//.......... ???????????}else if(httpResponse.getStatusLine().getStatusCode() == 500){ ???????????????//............. ???????????} ???????} catch (IOException e) { ???????????e.printStackTrace(); ???????}finally { ???????????try { ???????????????httpCilent2.close(); ???????????} catch (IOException e) { ???????????????e.printStackTrace(); ???????????} ???????}

3.POST请求,参数

//获取可关闭的 httpCilent ???????CloseableHttpClient httpClient = HttpClients.createDefault(); ???????//配置超时时间 ???????RequestConfig requestConfig = RequestConfig.custom(). ???????????????setConnectTimeout(1000).setConnectionRequestTimeout(1000) ???????????????.setSocketTimeout(1000).setRedirectsEnabled(true).build(); ????????????????HttpPost httpPost = new HttpPost("http://consentprt.dtac.co.th/webaoc/123SubscriberProcess"); ???????//设置超时时间 ???????httpPost.setConfig(requestConfig); ???????//装配post请求参数 ???????List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); ????????list.add(new BasicNameValuePair("age", "20")); ?//请求参数 ???????list.add(new BasicNameValuePair("name", "zhangsan")); //请求参数 ???????try { ???????????UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8"); ????????????//设置post求情参数 ???????????httpPost.setEntity(entity); ???????????HttpResponse httpResponse = httpClient.execute(httpPost); ???????????String strResult = ""; ???????????if(httpResponse != null){ ????????????????System.out.println(httpResponse.getStatusLine().getStatusCode()); ???????????????if (httpResponse.getStatusLine().getStatusCode() == 200) { ???????????????????strResult = EntityUtils.toString(httpResponse.getEntity()); ???????????????} else if (httpResponse.getStatusLine().getStatusCode() == 400) { ???????????????????strResult = "Error Response: " + response.getStatusLine().toString(); ???????????????} else if (httpResponse.getStatusLine().getStatusCode() == 500) { ???????????????????strResult = "Error Response: " + response.getStatusLine().toString(); ???????????????} else { ???????????????????strResult = "Error Response: " + response.getStatusLine().toString(); ???????????????} ????????????}else{ ????????????????????????????} ???????????System.out.println(strResult); ???????} catch (Exception e) { ???????????e.printStackTrace(); ???????}finally { ???????????try { ???????????????if(httpClient != null){ ???????????????????httpClient.close(); //释放资源 ???????????????} ???????????} catch (IOException e) { ???????????????e.printStackTrace(); ???????????} ???????}

4.Post 请求,map参数

public static String doPost(String url, Map<String, Object> paramsMap){ ???????CloseableHttpClient httpClient = HttpClients.createDefault(); ???????HttpPost httpPost = new HttpPost(url); ???????RequestConfig requestConfig = RequestConfig.custom(). ???????????????setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000) ???????????????.setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build(); ???????httpPost.setConfig(requestConfig); ????????????????List<NameValuePair> nvps = new ArrayList<NameValuePair>(); ???????for (String key : paramsMap.keySet()) { ???????????nvps.add(new BasicNameValuePair(key, String.valueOf(paramsMap.get(key)))); ???????} ???????try { ???????????httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); ???????????logger.info("httpPost ===**********===>>> " + EntityUtils.toString(httpPost.getEntity())); ???????????HttpResponse response = httpClient.execute(httpPost); ???????????String strResult = ""; ???????????if (response.getStatusLine().getStatusCode() == 200) { ????????????????strResult = EntityUtils.toString(response.getEntity()); ????????????????return strResult; ???????????} else { ???????????????return "Error Response: " + response.getStatusLine().toString(); ???????????} ???????} catch (Exception e) { ???????????e.printStackTrace(); ???????????return "post failure :caused by-->" + e.getMessage().toString(); ???????}finally { ???????????if(null != httpClient){ ???????????????try { ???????????????????httpClient.close(); ???????????????} catch (IOException e) { ???????????????????e.printStackTrace(); ???????????????} ???????????} ???????} ???}

5.POST 请求,参数是json字符串

public static String doPostForJson(String url, String jsonParams){ ???????????CloseableHttpClient httpClient = HttpClients.createDefault(); ???????????HttpPost httpPost = new HttpPost(url); ???????????RequestConfig requestConfig = RequestConfig.custom(). ???????????????????setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000) ???????????????????.setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build(); ????????????????????????httpPost.setConfig(requestConfig); ???????????httpPost.setHeader("Content-Type","application/json"); ?// ???????????try { ???????????????httpPost.setEntity(new StringEntity(jsonParams,ContentType.create("application/json", "utf-8"))); ???????????????System.out.println("request parameters" + EntityUtils.toString(httpPost.getEntity())); ???????????????HttpResponse response = httpClient.execute(httpPost); ???????????????System.out.println(" code:"+response.getStatusLine().getStatusCode()); ???????????????System.out.println("doPostForInfobipUnsub response"+response.getStatusLine().toString()); ???????????????return String.valueOf(response.getStatusLine().getStatusCode()); ???????????} catch (Exception e) { ???????????????e.printStackTrace(); ???????????????return "post failure :caused by-->" + e.getMessage().toString(); ???????????}finally { ???????????????if(null != httpClient){ ???????????????????try { ???????????????????????httpClient.close(); ???????????????????} catch (IOException e) { ???????????????????????e.printStackTrace(); ???????????????????} ???????????????} ???????????}}

HttpClient 4.5.3( get和post请求)

原文地址:https://www.cnblogs.com/heqiyoujing/p/10416008.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved