HttpClient发送Post请求(一)
来源:互联网 发布:中国高铁网络 编辑:程序博客网 时间:2024/05/18 23:54
pom
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version></dependency>
code
package com.xxxxxx.xxxxx; import java.io.IOException;import java.nio.charset.Charset;import org.apache.http.HttpEntity;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;/** * @author wushucheng * @version 创建时间:2017年6月28日 下午5:20:23 * @TODO 类说明 */public class HttpUtil { public static String post(String url, String body){ String result = null; CloseableHttpClient client = null; CloseableHttpResponse response = null; try { client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(body, Charset.forName("UTF-8")); httpPost.setEntity(stringEntity); response = client.execute(httpPost); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ if(response != null){ try { response.close(); } catch (IOException e) { e.printStackTrace(); } } if(client != null){ try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }}
attention
以上代码可以发送 http/https post 请求
调用微信API的时候,若body存在中文,一定要进行 UTF-8 转码,否则会报错
阅读全文
0 0
- HttpClient发送Post请求(一)
- Httpclient发送POST请求
- httpClient发送post请求
- HttpClient发送post请求
- httpclient发送post请求
- HttpClient 发送 POST 请求
- httpclient发送post请求
- httpClient发送post请求
- httpclient 发送post请求
- HttpClient发送Post请求(二)
- 用HttpClient发送post请求
- 使用HttpClient发送POST请求
- 使用httpclient发送post请求
- java发送post和get请求(1)---HttpClient 方式
- HttpClient 发送GET和POST请求(HTTP)
- java httpclient发送post get 请求
- HttpClient发送get和post请求
- android使用apache httpclient发送post请求
- C++ 类型转换函数 与 explicit
- 图片加载失败或默认图片
- 微信小程序从注册到上线系列
- SpreadJS 在 Angular2 中支持绑定哪些属性?
- STM8S---选项字节(Option Byte)写操作之IO复用
- HttpClient发送Post请求(一)
- PX PT 磅 EM 以及PS中字间距VA单位的解释
- HBase性能调优(1.2官方文档)
- Maven,Gradle分别建立Spring-boot的demo工程
- 使用react-native-smart-splash-screen实现启动画面
- printf()函数和浮点数
- 增加VOICE_CALL的默认音量
- C++ API设计—— API简介
- 29 字符提取命令 cut、printf、awk、sed