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里面的数据就是编辑完之后的内容了
随机推荐
- [vue]vue-book
我们打算要做这几个模块 首页 列表 收藏 添加 home.vue --> list.vue -->app.vue --> main.js 安装环境 npm i vue-cli -g ...
- poj2063 Investment
http://poj.org/problem?id=2063 首先总结一下:总的来说通过这题我深深感觉到了自己的不足,比赛时思维很受限,...面对超时,没有想到好的解决方案. 题意:给出初始资金,还有 ...
- Git简单入门教程
1.下载Git,360的软件管家里搜 2.安装Git,下载好之后安装到指定路径下 安装方法有多个复选框的把第一个也选上,其他默认直接next,最后一步什么都不选 3.配置用户信息:(右键-->g ...
- ev3_basic——HITCON CTF 2018
[MISC] EN-US This challenge provides a jpg file and a pklg file. The jpg is shown a part of string o ...
- soapUI-Conditional Goto
1.1.1 Conditional Goto 1.1.1.1 概述 - Conditional Goto Conditional Goto TestStep包含任意数量的XPath/JSONPath ...
- np.repeat 与 np.tile
1.Numpy的 tile() 函数,就是将原矩阵横向.纵向地复制.tile 是瓷砖的意思,顾名思义,这个函数就是把数组像瓷砖一样铺展开来. 举个例子,原矩阵: import numpy as np ...
- Vagrant配置虚拟机
慕课上学习.需要安装 vagrant VirtualBox .box文件和.iso文件一样都是镜像文件.可以在官网下载https://www.vagrantup.com/docs/ 点击boxs之后 ...
- BCB 按钮添加背景图
使用控件:TBitBtn 位于 Additional分类 属性:GlyPh
- css rgba透明度变化
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Asp.net Mvc5的认识
前言:以前总说自己玩mvc,但是对mvc的认识还是不够透彻,也没有好好看微软自带的mvc项目中的精妙,最近闲了下来,好好看了看. 通过上图,我们可以清晰地了解到MVC 5应用程序的项目结构,接下来我们 ...