用httpClient 发送消息,报错,但是我的agentId确实写了,原来是格式不对。
package bridge;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.Consts;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.json.JSONObject;
public class WeiXinDemo {
JSONObject a;
public String tokenStr(){
//发送token
String tokenUrl="https://qyapi.weixin.qq.com/cgi-bin/gettoken?&corpid=你自己的&corpsecret=你自己的;
CloseableHttpClient httpGetClient=HttpClients.createDefault();
HttpGet httpGet=new HttpGet(tokenUrl);
RequestConfig config = RequestConfig.custom().setSocketTimeout(5000).build();
httpGet.setConfig(config);
CloseableHttpResponse responseToken;
try {
responseToken = httpGetClient.execute(httpGet);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(responseToken);
System.out.println(responseContent);
a=new JSONObject(responseContent);
} catch (ClientProtocolException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
return (String) a.get("access_token");
}
public String sendMsg(String token){
String responseContent="";
String url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+""+token+"";
String strData;
String strTest="{'touser':'微信号','msgtype':'news','toparty':'','totag':'','agentid':'81','safe':'0','news':{'articles':[{'title':'有测试','url':'你的域名地址','picurl':null}]}}";
//就是这个的格式错误,这里最后必须替换为双引号才能识别出来。其实最简单的方法是新建一个对象用来放数据,最后转为json 字符串比较方便
strData=strTest.replace('\'', '\"');
CloseableHttpClient httpClient = HttpClients.createDefault() ;
HttpPost httpPost=new HttpPost(url);
RequestConfig requestConfig=RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(5000).build();
httpPost.setConfig(requestConfig);
httpPost.addHeader("Content-Type", "application/json");
StringEntity entity=new StringEntity(strData,Consts.UTF_8);
httpPost.setEntity(entity);
CloseableHttpResponse response;
try {
response = httpClient.execute(httpPost, new BasicHttpContext());
responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
System.out.println(responseContent);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return responseContent;
}
//如何调用微信发送消息的接口
public static void main(String args[]){
WeiXinDemo demo=new WeiXinDemo();
demo.sendMsg(demo.tokenStr());
}
}
import java.io.IOException;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;
public class Utf8ResponseHandler implements ResponseHandler<String> {
public static final ResponseHandler<String> INSTANCE = new Utf8ResponseHandler();
public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
final HttpEntity entity = response.getEntity();
return entity == null ? null : EntityUtils.toString(entity, Consts.UTF_8);
}
}