packagecom.baosight.wechat.service;
importnet.sf.json.JSONObject;
importorg.apache.commons.httpclient.HttpStatus;
importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.entity.StringEntity;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.util.EntityUtils;
importorg.junit.Test;
importcom.baosight.wechat.project.baosightecmp.ConstantUtilEcmp;
importcom.baosight.wechat.util.HttpUtil;
{
@Test
{
String Access_token = HttpUtil.getAccess_token_server(ConstantUtilEcmp.APPID, ConstantUtilEcmp.APPSECRET);
// String open_id = "oe7rSjlz1flhx7HP3-DnlgrpobqM";
JSONObject obj = JSONObject.fromObject(Access_token);
String token = obj.getString("access_token");
String strJson ="{\"touser\" :\"oe7rSjlz1flhx7HP3-DnlgrpobqM\",";
strJson +="\"msgtype\":\"text\",";
strJson +="\"content\":\"Hello World\"";
System.out.println(url);
}
publicvoidpost(String url, String json)
{
DefaultHttpClient client =newDefaultHttpClient();
HttpPost post =newHttpPost(url);
try
{
StringEntity s =newStringEntity(json);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
HttpResponse res = client.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
HttpEntity entity = res.getEntity();
System.out.println(EntityUtils.toString(entity,"utf-8"));
}
}
{
thrownewRuntimeException(e);
}
}
}<br />