http post发送请求
2024-10-01 02:29:35
一: 用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;
}
}
最新文章
- Mac系统下React Native环境搭建
- (l老陈-小石头)典型用户、用户故事、用例图
- Makefile学习笔记
- javascript版Ajax请求
- 使用IE浏览器下载时候窗口一闪而过
- 支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url.
- window.showModalDialog 子窗口和父窗口不兼容最新的谷歌
- Entity Framewor 学习笔记 (Enum)
- 被误解的 MVC 和被神化的 MVVM
- Light oj 1030 概率DP
- python交换两个变量的值,一句代码搞定
- T4模版引擎之基础入门
- Codeforces Round #410 (Div. 2)D题
- CJOJ 2022 【一本通】简单的背包问题(搜索)
- Luogu P1231 教辅的组成
- 【GMT43智能液晶模块】基于HAL库的SDRAM和LCD驱动例程(MDK工程&;CubeMX工程)
- mysql 问题:wait_timeout
- 转:CMake 使用方法
- CP2102
- 【BZOJ1054】[HAOI2008]移动玩具 BFS
热门文章
- Kylin安装问题--/home/hadoop-2.5.1/contrib/capacity-scheduler/.jar (No such file or directory)
- 查看Django和flask版本
- 优化Ubuntu 16.04系统的几件事
- CloudFlare Support - Error 522: Connection timed out 错误522:连接超时
- 数据分析与挖掘 - R语言:K-means聚类算法
- sqlplus与shell互相传值的几种情况
- ArcGIS 10——版本编辑流程
- idea 上搭建 Mybatis 逆向工程
- linux常用命令:ifconfig 命令
- Android :64位支持的说明