概述
package cn.jiankang51.hsps.svc.app.chat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import cn.jiankang51.hsps.pay.util.Sha1Util;
import net.sf.json.JSONObject;
/**
*
* ChatHttpClientUtils
* 与融云的http通信工具类
* 1.post请求
* 2.get请求
* AL
* 2015年5月15日 上午9:31:23
*
* @version 1.0.0
*
*/
public class ChatHttpClientUtils
{
public static final String APP_KEY = "pwe86ga5eljj6";
public static final String APP_SECRET = "eEv9vNiS1y5Qrh";
private static final Logger logger = Logger.getLogger(ChatHttpClientUtils.class);
public static void wrapRequestHeader(HttpPost post)
{
String nonceStr = Sha1Util.getNonceStr();
String timeStamp = Sha1Util.getTimeStamp();
String signature = Sha1Util.getSha1(APP_SECRET + nonceStr + timeStamp);
post.setHeader("App-Key", APP_KEY);
post.setHeader("Nonce", nonceStr);
post.setHeader("Timestamp", timeStamp);
post.setHeader("Signature", signature);
/*Set<Entry<String, String>> entrySet = reqJson.entrySet();
//设置参数
for(Entry<String, String> entry : entrySet){
post.setParameter(entry.getKey(), entry.getValue());
}*/
}
/**
* 调用 API
* @param parameters
* @return
*/
@SuppressWarnings("unchecked")
public static String post(JSONObject reqJson, String uri)
{
String body = null;
HttpPost method = new HttpPost(uri);
HttpClient httpClient = new DefaultHttpClient();
logger.info("json:" + reqJson.toString());
try
{
wrapRequestHeader(method);
List<NameValuePair> params = new ArrayList<NameValuePair>();
//建立一个NameValuePair数组,用于存储欲传送的参数
Set<Entry<String, String>> set = reqJson.entrySet();
for(Entry<String, String> entry : set){
params.add(new BasicNameValuePair(entry.getKey(),entry.getValue() ));
}
//添加参数
method.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//设置编码
HttpResponse response = httpClient.execute(method);
int statusCode = response.getStatusLine().getStatusCode();
logger.info("statusCode:" + statusCode);
if (statusCode != HttpStatus.SC_OK)
{
logger.error("Method failed:" + response.getStatusLine());
}
//Read the response body
body = EntityUtils.toString(response.getEntity());
}
catch (IOException e)
{
//发生网络异常
logger.error("exception occurred!n" + ExceptionUtils.getFullStackTrace(e));
//网络错误
}
return body;
}
public static void main(String[] args)
{
// System.out.println(Sha1Util.getSha1("eEv9vNiS1y5Qrh"+"910655241"+"1431662048"));
JSONObject reqJson = new JSONObject();
reqJson.put("userId", "0034");
System.out.println(post(reqJson, "https://api.cn.rong.io/user/getToken.json"));
}
}
最后
以上就是美满皮卡丘为你收集整理的HTTPPOST 发送JSON格式参数的全部内容,希望文章能够帮你解决HTTPPOST 发送JSON格式参数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复