http post发送请求
一: 用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;
}
}
http post发送请求的更多相关文章
- RestTemplate发送请求并携带header信息
1.使用restTemplate的postForObject方法 注:目前没有发现发送携带header信息的getForObject方法. HttpHeaders headers = new Http ...
- zookeeper源码分析之三客户端发送请求流程
znode 可以被监控,包括这个目录节点中存储的数据的修改,子节点目录的变化等,一旦变化可以通知设置监控的客户端,这个功能是zookeeper对于应用最重要的特性,通过这个特性可以实现的功能包括配置的 ...
- 用Retrofit发送请求中添加身份验证
用Retrofit发送请求中添加身份验证====================在安卓应用开发中, retrofit可以极大的方便发送http网络请求,不管是GET, POST, 还是PUT, DEL ...
- ajax-向服务器发送请求
ajax-向服务器发送请求 1.将请求发送到服务器,使用XMLHttpRequest对象的 open() 和 send() 方法. xmlhttp. open(method,url,async ...
- Android HTTP实例 使用GET方法和POST方法发送请求
Android HTTP实例 使用GET方法和POST方法发送请求 Web程序:使用GET和POST方法发送请求 首先利用MyEclispe+Tomcat写好一个Web程序,实现的功能就是提交用户信息 ...
- Android HTTP实例 发送请求和接收响应
Android HTTP实例 发送请求和接收响应 Android Http连接 实例:发送请求和接收响应 添加权限 首先要在manifest中加上访问网络的权限: <manifest ... & ...
- PHP发送请求头和接收打印请求头
一.发送请求头 //发送地址 $url = 'http://127.0.0.1/2.php'; //请求头内容 $headers = array( 'Authorization: '.$basic, ...
- URLConnection 和 HttpClients 发送请求范例
. java.net.URLConnection package test; import java.io.BufferedReader; import java.io.IOException; im ...
- Query通过Ajax向PHP服务端发送请求并返回JSON数据
Query通过Ajax向PHP服务端发送请求并返回JSON数据 服务端PHP读取MYSQL数据,并转换成JSON数据,传递给前端Javascript,并操作JSON数据.本文将通过实例演示了jQuer ...
- 将textField编辑完内容作为参数发送请求
将textField编辑完内容作为参数发送请求 首先赋值默认值 其次把编辑完的内容传给model,这样的话,model里面的数据就是编辑完之后的内容了
随机推荐
- 使用gunicorn部署Flask项目
[*] 本文出处:http://b1u3buf4.xyz/ [*] 本文作者:B1u3Buf4 [*] 本文授权:禁止转载 从自己的博客移动过来. gunicorn是一个python Wsgi的WEB ...
- isKindOfClass isMemeberOfClass 的区分
isKindOfClass If you use such constructs in your code, you might think it is alright to modify an ob ...
- js时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- mybitis学习笔记
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-// ...
- VS2010/MFC编程入门之四十(文档、视图和框架:各对象之间的关系)
前面一节中鸡啄米进行了文档.视图和框架的概述,本节主要讲解文档.视图.框架结构中各对象之间的关系. 各个对象之间的关系 文档.视图.框架结构中涉及到的对象主要有:应用程序对象.文档模板对象.文档对象. ...
- 由浅入深之Tensorflow(3)----数据读取之TFRecords
转载自http://blog.csdn.net/u012759136/article/details/52232266 原文作者github地址 概述 关于Tensorflow读取数据,官网给出了三种 ...
- iperf3.0 hisi uclib 交叉编译
1. 下载iperf src https://github.com/esnet/iperf/ 2.修改makefile.in 里面的配置. src/Makefile.in 613行 地方两行,去掉-p ...
- 学写网页 #04# w3school
索引: HTML 输入类型 XHTML HTML5 HTML5 样式指南和代码约定 WHO 成立于 1948 年. 对缩写进行标记能够为浏览器.翻译系统以及搜索引擎提供有用的信息. code 元素不保 ...
- phpstudy composer 使用安装
本人是windows 系统 phpstudy 是最新2018版本 以安装laravel框架为例子 一如图一,点击php Composer出现系统指令框,根据指令框路径找到文件 二把红框内文件删除 三在 ...
- 根据wsdl,基于wsimport生成代码的客户端
根据wsdl,基于wsimport生成代码的客户端 wsimport是jdk自带的命令,可以根据wsdl文档生成客户端中间代码,基于生成的代码编写客户端,可以省很多麻烦. 局限性:wsimport ...