Java HttpClient Post多层json格式参数
来源:互联网 发布:openwrt 挂载网络共享 编辑:程序博客网 时间:2024/05/22 14:10
package ...
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpWeiXinMessage {
public static String WeiXinMessage(String url,Map<String,String> params) throws ClientProtocolException, IOException{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//请求头
/*方法一:一层json*/
//StringEntity stringEntity = new StringEntity(data,"UTF-8");
//httpPost.setEntity(stringEntity);//请求主体
/*方法二*:多层json*/
HttpWeiXinMessage.setPostData(httpPost, params);
HttpResponse httpResponse = httpClient.execute(httpPost);//发送请求
HttpEntity httpEntity = httpResponse.getEntity();//获取请求返回体
String backResult = EntityUtils.toString(httpEntity,"UTF-8");//请求返回结果
if(httpResponse != null){
try{
EntityUtils.consume(httpResponse.getEntity());
}catch(IOException e){
e.printStackTrace();
}
}//释放资源
return backResult;
}
//设置请求参数
private static void setPostData(HttpPost httpPost, Map<String,String> params){
/*另外一种设置请求参数方法*/
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
HttpEntity httpEntity = null;
try {
httpEntity = new UrlEncodedFormEntity(list, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpPost.setEntity(httpEntity);//设置请求主体
}
//传递Map<String,Strnig> params参数格式
Map<String, Strnig> map = new LinkedHashMap<String, String>();
map.put("contexcode", "log");
map.put("source", "weixin");
map.put("form", "WeChat");
map.put("messageId", "jjtz");
map.put("data", "{'touser':" + amv.getOpenId() + ",'data':{'first': {'value':'qjtz','color':'#0'}," +
"'keyword1':{'value':'"+realName+"','color':'#0'},'keyword2': {'value':'"+phone+"','color':'#173177'}," +
"'remark': {'value':'"+senderAddress+"','color':'#173177'}}}");
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpWeiXinMessage {
public static String WeiXinMessage(String url,Map<String,String> params) throws ClientProtocolException, IOException{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//请求头
/*方法一:一层json*/
//StringEntity stringEntity = new StringEntity(data,"UTF-8");
//httpPost.setEntity(stringEntity);//请求主体
/*方法二*:多层json*/
HttpWeiXinMessage.setPostData(httpPost, params);
HttpResponse httpResponse = httpClient.execute(httpPost);//发送请求
HttpEntity httpEntity = httpResponse.getEntity();//获取请求返回体
String backResult = EntityUtils.toString(httpEntity,"UTF-8");//请求返回结果
if(httpResponse != null){
try{
EntityUtils.consume(httpResponse.getEntity());
}catch(IOException e){
e.printStackTrace();
}
}//释放资源
return backResult;
}
//设置请求参数
private static void setPostData(HttpPost httpPost, Map<String,String> params){
/*另外一种设置请求参数方法*/
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
HttpEntity httpEntity = null;
try {
httpEntity = new UrlEncodedFormEntity(list, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpPost.setEntity(httpEntity);//设置请求主体
}
//传递Map<String,Strnig> params参数格式
Map<String, Strnig> map = new LinkedHashMap<String, String>();
map.put("contexcode", "log");
map.put("source", "weixin");
map.put("form", "WeChat");
map.put("messageId", "jjtz");
map.put("data", "{'touser':" + amv.getOpenId() + ",'data':{'first': {'value':'qjtz','color':'#0'}," +
"'keyword1':{'value':'"+realName+"','color':'#0'},'keyword2': {'value':'"+phone+"','color':'#173177'}," +
"'remark': {'value':'"+senderAddress+"','color':'#173177'}}}");
}
0 0
- Java HttpClient Post多层json格式参数
- JAVA HttpClient(普通参数、JSON格式参数)
- httpclient的几种post参数格式
- httpclient使用post提交json参数
- JAVA HTTP POST请求(请求参数格式为json)
- post请求中参数为json格式
- http post 请求参数格式是json
- 关于HttpClient post请求 传输json格式的字节流
- httpclient的post请求并处理json格式响应数据
- HTTPCLIENT POST JSON
- java httpclient POST和GET请求 发送JSON
- java封装多层json
- httpclient提交json参数
- httpclient提交json参数
- HttpClient与参数json
- Java Post请求 传递Json参数
- HttpClient4.3 用post方式传JSON格式字符串参数
- ExtJS发送POST请求 参数格式为JSON
- Android Studio 2.1 官方下载地址
- java中this,static关键词的用法
- SDRAM容量计算
- php实现无限级分类
- Cpanel如何使用phpMyAdmin
- Java HttpClient Post多层json格式参数
- 牛人主页(主页有很多论文代码)【真的好强大】
- iOS高阶面试题之某某音乐的面试题
- 不懂技术的人不要对懂技术的人说这很容易实现
- 什么是CSS?&CSS的三种使用方式
- MFC 句柄获取
- test
- java中的继承
- Linux入门使用-5