httpclient
来源:互联网 发布:程序员薪资城市排行榜 编辑:程序博客网 时间:2024/06/06 14:02
post
import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils; // http请求 public static Result Post(String path, Map<String, String> param) { String url = "http://192.168.1.115:9191" + path; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); Map<String, String> headerMap = new HashMap<>(); headerMap.put("Host", "****"); headerMap.put("Accept-Encoding", "gzip"); headerMap.put("charset", "utf-8"); List<NameValuePair> pList = new ArrayList<>(); for (Map.Entry<String, String> entry : headerMap.entrySet()) { pList.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } for (Map.Entry<String, String> entry : param.entrySet()) { pList.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pList, "UTF-8"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); HttpEntity resEntity = response.getEntity(); String content = ""; if (resEntity != null) { content = EntityUtils.toString(resEntity, "UTF-8"); } Result res = new Gson().fromJson(content, Result.class); return res; } catch (Exception e) { String msg = "请求数说读者的GO服务失败, url: " + url; LogDefine.LOG.error(msg, e); return new Result<String>(null, false, msg, null); } }
阅读全文
0 0
- HttpClient
- HttpClient
- httpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- HttpClient
- httpclient
- httpclient
- HttpClient
- HttpClient
- httpclient
- httpclient
- HttpClient
- 大数据正式20
- 经典算法---堆排序(Heapsort)
- IntelliJ IDEA (2017.3)激活
- Django中Mixin和View组合技巧
- java实现emqtt的消息发布
- httpclient
- HDOJ1405 The Last Practice(有坑)
- 2. 什么是对象
- G盘显示拒绝访问要如何办啊
- react-native-swiper 让人无语的坑
- 虚拟网卡 tun/tap口创建及介绍
- 郁闷的出纳员
- the mathtype dll cannot be found word2010(word2013类似)
- Python--模块之optparse