一:

用java自带URL发送
public synchronized JSONObject getJSON(String url2, String param) {
try {
URL url = new URL(url2);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true); //获取返回数据需要设置为true 默认false
con.setDoInput(true); //发送数据需要设置为true 默认false
con.setReadTimeout(5000);
con.setConnectTimeout(5000);
con.setRequestMethod("POST");
con.connect();
DataOutputStream out = new DataOutputStream(con.getOutputStream());
if (param != null) {
param = URLEncoder.encode(param,"utf-8");//url编码防止中文乱码
out.writeBytes(param);
}
out.flush();
out.close();
BufferedReader red = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
StringBuffer sb = new StringBuffer();
String line;
while ((line = red.readLine()) != null) {
sb.append(line);
}
red.close();
return JSONObject.fromObject(sb);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} 二:apache httppost方式 /**
* post请求,发送json数据
*
* @param url
* @param json
* @return
*/
public static JSONObject doPost(String url, String json) {
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(json, "UTF-8"); // 中文乱码在此解决
s.setContentType("application/json");
post.setEntity(s);
HttpResponse res = HttpClients.createDefault().execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
response = JSON.parseObject(result);
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
} 三: httppost 发送map /**
* post请求
*
* @param url
* @param param
* @return
*/
public static String httpPost(String url, Map<String, Object> map) {
try {
HttpPost post = new HttpPost(url);
        //requestConfig post请求配置类,设置超时时间
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(50000).build();
post.setConfig(requestConfig);
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() != null && entry.getValue() != "") {
              //用basicNameValuePair来封装数据
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue() + ""));
}
}
          //在这个地方设置编码 防止请求乱码
post.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
GeneralLog.info(ModelName, "请求url:" + url);
GeneralLog.info(ModelName, "请求数据:" + map);
CloseableHttpResponse httpResponse = HttpClients.createDefault().execute(post);
System.out.println("返回数据:" + httpResponse);
String result = null;
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);// 取出应答字符串
}
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

最新文章

  1. Mac系统下React Native环境搭建
  2. (l老陈-小石头)典型用户、用户故事、用例图
  3. Makefile学习笔记
  4. javascript版Ajax请求
  5. 使用IE浏览器下载时候窗口一闪而过
  6. 支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url.
  7. window.showModalDialog 子窗口和父窗口不兼容最新的谷歌
  8. Entity Framewor 学习笔记 (Enum)
  9. 被误解的 MVC 和被神化的 MVVM
  10. Light oj 1030 概率DP
  11. python交换两个变量的值,一句代码搞定
  12. T4模版引擎之基础入门
  13. Codeforces Round #410 (Div. 2)D题
  14. CJOJ 2022 【一本通】简单的背包问题(搜索)
  15. Luogu P1231 教辅的组成
  16. 【GMT43智能液晶模块】基于HAL库的SDRAM和LCD驱动例程(MDK工程&amp;CubeMX工程)
  17. mysql 问题:wait_timeout
  18. 转:CMake 使用方法
  19. CP2102
  20. 【BZOJ1054】[HAOI2008]移动玩具 BFS

热门文章

  1. Kylin安装问题--/home/hadoop-2.5.1/contrib/capacity-scheduler/.jar (No such file or directory)
  2. 查看Django和flask版本
  3. 优化Ubuntu 16.04系统的几件事
  4. CloudFlare Support - Error 522: Connection timed out 错误522:连接超时
  5. 数据分析与挖掘 - R语言:K-means聚类算法
  6. sqlplus与shell互相传值的几种情况
  7. ArcGIS 10——版本编辑流程
  8. idea 上搭建 Mybatis 逆向工程
  9. linux常用命令:ifconfig 命令
  10. Android :64位支持的说明