httppost

package com.kmt.cn;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang.StringUtils;
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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
//import org.springframework.util.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
//import com.cluster.server.message.Result;
import com.google.gson.JsonObject;
import com.kmt.cn.SHA1Util;
/*import com.pojo.ExportResult;
import com.pojo.Members;*/

public class Test1 {

public static void main(String[] args) {
String urlString ="https://api.netease.im/nimserver/user/refreshToken.action";
//
Map<String, Object> map = new HashMap<String, Object>();
// map.put("accid", 184644);
// String resultString= Test1.post(urlString, map);
// System.out.println("result="+resultString);
//
//
//
       urlString ="http://api.netease.im/nimserver/msg/sendMsg.action";

      
      
map = new HashMap<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 = new HashMap<String, Object>();
map.put("accid", "184737"); //用户ID
map.put("name", "系统通知"); // 昵称
map.put("token", "111111"); //密码
//System.out.println("result="+Test1.post(urlString, map));

}

/**
* POST请求
*
* @param url URL
* @param parameterMap 请求参数
* @return 返回结果
*/
public static String post(String url, Map<String, Object> parameterMap) {
//Assert.hasText(url);
String result = null;
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost httpPost = new HttpPost(url);
String ctimeString= 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 = new ArrayList<NameValuePair>();
if (parameterMap != null) {
for (Entry<String, Object> entry : parameterMap.entrySet()) {
String name = entry.getKey();
String value = ConvertUtils.convert(entry.getValue());
if (StringUtils.isNotEmpty(name)) {
nameValuePairs.add(new BasicNameValuePair(name, value));
}
}
}
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return result;
}
}

你可能感兴趣的:(httppost)