HttpComponent-HttpClient使用-发送Post请求
2016-06-28 15:02
501 查看
public class ElephantServiceImpl implements ElephantService { public final static String url = "http://dxw-in.sankuai.com/api/pub/push"; public final static String client_id = "xxxxxxxxxx"; public final static String client_secret = "xxxxxxxxxx"; public boolean send(List<String> receivers, String body, String sender, String type) { try { String rfc1123Date = SignatureUtils.getRfc1123Pattern(); String authorization = SignatureUtils.getAuthorizationHeader(rfc1123Date,url,client_id,client_secret); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json;charset=utf-8"); httpPost.setHeader("Date",rfc1123Date); httpPost.setHeader("Authorization",authorization); String json = constructJson(receivers, body, sender, type); StringEntity paraEntity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(paraEntity); CloseableHttpResponse response = httpclient.execute(httpPost); String responseContent = null; // 响应内容 if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); System.out.println(responseContent); } response.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } private String constructJson(List<String> receivers, String body, String sender, String type) { String json = "{\"sender\":\"" + sender + "\", \"receivers\":" + receivers.toString() + ", \"body\": \"" + body + "\",\"messageType\":\"" + type + "\"} "; return json; } }
相关文章推荐
- Python中asyncore异步模块的用法及实现httpclient的实例
- 监控自己APP的http/https网络请求的地址和请求耗时
- iOS--NSNetService和NSNetServiceBrowser(Bonjour网络编程)
- 大师的错误?UNP1中raw socket不能接收TCP和UDP的错误
- OkHttp3源码分析[任务队列]
- pip install Error - ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read
- iOS 偶说获取网络请求到的图片尺寸
- 用开源工具Xplico助力网络应用层数据解码
- 简单介绍HttpURLConnection请求网络以及AsyncTask异步任务的用法
- @font-face的用法 WebFont网络字体
- Android网络请求框架--AsyncHttpClient
- Http协议
- Curl http_code 状态码 意义及信息
- SSH 远程登录 基础知识
- @font-face 网络字体的中文用法(一)
- java代码模拟http请求
- httpd设置HTTPS双向认证
- 网络协议_计算机网络_本机ip、127.0.0.1和0.0.0.0区别
- VMware 虚拟机下网络配置基础
- Retrofit网络通信库简单封装使用