httppost

packagecom.kmt.cn;

importjava.io.IOException;

importjava.util.ArrayList;

importjava.util.Arrays;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importjava.util.Map.Entry;

importorg.apache.commons.beanutils.ConvertUtils;

importorg.apache.commons.lang.StringUtils;

importorg.apache.http.HttpEntity;

importorg.apache.http.HttpResponse;

importorg.apache.http.NameValuePair;

importorg.apache.http.client.ClientProtocolException;

importorg.apache.http.client.HttpClient;

importorg.apache.http.client.entity.UrlEncodedFormEntity;

importorg.apache.http.client.methods.HttpPost;

importorg.apache.http.impl.client.DefaultHttpClient;

importorg.apache.http.message.BasicNameValuePair;

importorg.apache.http.util.EntityUtils;

//importorg.springframework.util.Assert;

importcom.alibaba.fastjson.JSON;

importcom.alibaba.fastjson.JSONObject;

importcom.alibaba.fastjson.serializer.SerializerFeature;

//importcom.cluster.server.message.Result;

importcom.google.gson.JsonObject;

importcom.kmt.cn.SHA1Util;

/*importcom.pojo.ExportResult;

importcom.pojo.Members;*/

publicclassTest1{

publicstaticvoidmain(String[]args){

StringurlString="https://api.netease.im/nimserver/user/refreshToken.action";

//

Map<String,Object>map=newHashMap<String,Object>();

//map.put("accid",184644);

//StringresultString=Test1.post(urlString,map);

//System.out.println("result="+resultString);

//

//

//

urlString="http://api.netease.im/nimserver/msg/sendMsg.action";

map=newHashMap<String,Object>();

map.put("from","100");

map.put("ope","0");

map.put("to","162763");

map.put("type","0");

map.put("body","{\"msg\":\"xxxxxxx\"}");

//System.out.println("result="+Test1.post(urlString,map));

//

//

//

urlString="https://api.netease.im/nimserver/msg/sendAttachMsg.action";

map.put("from","162763");

map.put("msgtype","0");

map.put("to","162763");

//map.put("attach","{\"msg\":\"XXX大夫您好,XXX用户申请退款,请及时处理\",\"id\":\"22213\"}");

map.put("attach","{\"msg\":\"XXX大夫您好,XXXXXX用户申请医生上门服务,请及时确认\",\"id\":\"22233\"}");

map.put("pushcontent","{\"msg\":\"XXX大夫您好,XXXXXX用户申请医生上门服务,请及时确认\",\"id\":\"22233\"}");

System.out.println("result="+Test1.post(urlString,map));

urlString="https://api.netease.im/nimserver/user/create.action";

map=newHashMap<String,Object>();

map.put("accid","184737");//用户ID

map.put("name","系统通知");//昵称

map.put("token","111111");//密码

//System.out.println("result="+Test1.post(urlString,map));

}

/**

*POST请求

*

*@paramurlURL

*@paramparameterMap请求参数

*@return返回结果

*/

publicstaticStringpost(Stringurl,Map<String,Object>parameterMap){

//Assert.hasText(url);

Stringresult=null;

HttpClienthttpClient=newDefaultHttpClient();

try{

HttpPosthttpPost=newHttpPost(url);

StringctimeString=System.currentTimeMillis()/1000+"";

httpPost.addHeader("AppKey","c47972b130b3098638608470c82982a3");

httpPost.addHeader("Nonce","1231313131312");

httpPost.addHeader("CurTime",ctimeString);

httpPost.addHeader("CheckSum",SHA1Util.SHA1Digest("abe20b110daf"+"1231313131312"+ctimeString));

List<NameValuePair>nameValuePairs=newArrayList<NameValuePair>();

if(parameterMap!=null){

for(Entry<String,Object>entry:parameterMap.entrySet()){

Stringname=entry.getKey();

Stringvalue=ConvertUtils.convert(entry.getValue());

if(StringUtils.isNotEmpty(name)){

nameValuePairs.add(newBasicNameValuePair(name,value));

}

}

}

httpPost.setEntity(newUrlEncodedFormEntity(nameValuePairs,"UTF-8"));

HttpResponsehttpResponse=httpClient.execute(httpPost);

HttpEntityhttpEntity=httpResponse.getEntity();

result=EntityUtils.toString(httpEntity);

EntityUtils.consume(httpEntity);

}catch(ClientProtocolExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}finally{

httpClient.getConnectionManager().shutdown();

}

returnresult;

}

}

相关推荐