use of com.guhanjie.weixin.model.ErrorEntity in project weixin-boot by guhanjie.
the class TestGetAccessToken method main.
/**
* Method Name: main<br/>
* Description: [description]
* @author guhanjie
* @time 2016年9月3日 下午10:27:28
* @param args
*/
public static void main(String[] args) {
LOGGER.info("Starting to refresh access token...");
HttpGet get = null;
CloseableHttpResponse resp = null;
CloseableHttpClient client = null;
try {
client = HttpClients.createDefault();
String url = WeixinConstants.API_ACCESS_TOKEN;
url = url.replaceAll("APPID", "***");
url = url.replaceAll("APPSECRET", "***");
get = new HttpGet(url);
resp = client.execute(get);
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
HttpEntity entity = resp.getEntity();
String content = EntityUtils.toString(entity);
try {
LOGGER.debug("Got response:[{}]", content);
AccessToken at = JSONObject.parseObject(content, AccessToken.class);
// token = at.getAccess_token();
LOGGER.info("Success to refresh access token:[{}]", at);
} catch (Exception e) {
ErrorEntity err = JSONObject.parseObject(content, ErrorEntity.class);
LOGGER.error("Failed to refresh access token, errcode:[{}], errmsg:[{}].", err.getErrcode(), err.getErrmsg());
// refreshToken();
}
}
} catch (Exception e) {
LOGGER.error("Http error while refreshing access token", e);
} finally {
try {
if (resp != null)
resp.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (client != null)
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of com.guhanjie.weixin.model.ErrorEntity in project weixin-boot by guhanjie.
the class TestHttpUtil method main.
/**
* Method Name: main<br/>
* Description: [description]
* @author guhanjie
* @time 2016年10月11日 上午1:38:14
* @param args
*/
public static void main(String[] args) {
HttpGet get = null;
CloseableHttpClient client = null;
CloseableHttpResponse resp = null;
final String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=DYon2mr_xJAGKtRG-gwkh2q3PBJ3QIILlRx2XT-xZC8MmPm8bQ345aG8CttbQBD6kW1pqlKTAITpH8nFAdtvsogkdeqBwFW9rHDTcDYL3eDvALmlh5CLCRM_2TPcoLLAQFMeAEAFUB&openid=o_05Uwe4_9GGQ93ESXg27RCw6HqE&lang=zh_CN";
try {
client = HttpClients.createDefault();
LOGGER.debug("sending http get request[{}]...", url);
get = new HttpGet(url);
resp = client.execute(get);
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
HttpEntity entity = resp.getEntity();
LOGGER.debug("content-type: [{}]", entity.getContentType().getValue());
String content = EntityUtils.toString(entity, Charset.forName("UTF-8"));
LOGGER.debug("success to get response:[{}]", content);
ErrorEntity err = JSONObject.parseObject(content, ErrorEntity.class);
if (err.getErrcode() != null || err.getErrmsg() != null) {
LOGGER.error("http[{}] response is error, errcode:[{}], errmsg:[{}].", url, err.getErrcode(), err.getErrmsg());
return;
}
LOGGER.debug("success to finish http get request.");
} else {
LOGGER.error("failed to get http response, http status:[{}]", statusCode);
}
} catch (Exception e) {
LOGGER.error("failed to send http get request", e);
} finally {
HttpClientUtils.closeQuietly(resp);
HttpClientUtils.closeQuietly(client);
}
}
use of com.guhanjie.weixin.model.ErrorEntity in project weixin-boot by guhanjie.
the class MessageKit method sendKFMsg.
public static void sendKFMsg(String openids, String content) {
if (StringUtils.isBlank(openids)) {
LOGGER.warn("send order msg to KF, but no kf openids.");
return;
}
String[] ids = openids.split(",");
for (final String openid : ids) {
LOGGER.info("starting to send message to KF[{}]...", openid);
try {
String url = WeixinConstants.API_KF_SEND_MSG;
String str = new String("{" + " \"touser\":\"OPENID\"," + " \"msgtype\":\"text\"," + " \"text\":" + " {" + " \"content\":\"ContentText\"" + " }" + "}");
str = str.replaceAll("OPENID", openid);
str = str.replaceAll("ContentText", content);
LOGGER.debug("message content: [{}]", str);
HttpEntity entity = new StringEntity(str, ContentType.APPLICATION_JSON);
WeixinHttpUtil.sendPost(url, entity, new WeixinHttpCallback() {
@Override
public void process(String json) {
ErrorEntity t = JSONObject.parseObject(json, ErrorEntity.class);
if (t != null && t.getErrcode() != null && t.getErrmsg() != null) {
if (t.getErrcode().equals("0") && t.getErrmsg().equals("ok"))
LOGGER.info("Success to post message to KF[{}].", openid);
return;
}
LOGGER.error("Failed to post message to KF, error:[{}].", json);
}
});
} catch (Exception e) {
LOGGER.error("error to post message[{}] to KF[{}].", content, openid);
}
}
}
use of com.guhanjie.weixin.model.ErrorEntity in project weixin-boot by guhanjie.
the class WeixinHttpUtil method sendGet.
public static void sendGet(String url, WeixinHttpCallback c) {
HttpGet get = null;
CloseableHttpClient client = null;
CloseableHttpResponse resp = null;
try {
client = HttpClients.createDefault();
url = url.replaceAll("ACCESS_TOKEN", AccessTokenKit.getToken());
LOGGER.debug("sending http get request[{}]...", url);
get = new HttpGet(url);
resp = client.execute(get);
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
HttpEntity entity = resp.getEntity();
String content = EntityUtils.toString(entity, Charset.forName("UTF-8"));
LOGGER.debug("success to get response:[{}]", content);
ErrorEntity err = JSONObject.parseObject(content, ErrorEntity.class);
if (err.getErrcode() != null || err.getErrmsg() != null) {
LOGGER.error("http[{}] response is error, errcode:[{}], errmsg:[{}].", url, err.getErrcode(), err.getErrmsg());
return;
}
c.process(content);
LOGGER.debug("success to finish http get request.");
} else {
LOGGER.error("failed to get http response, http status:[{}]", statusCode);
}
} catch (Exception e) {
LOGGER.error("failed to send http get request", e);
} finally {
HttpClientUtils.closeQuietly(resp);
HttpClientUtils.closeQuietly(client);
}
}
use of com.guhanjie.weixin.model.ErrorEntity in project weixin-boot by guhanjie.
the class WeixinHttpUtil method sendPost.
public static void sendPost(String url, HttpEntity entity, WeixinHttpCallback c) {
HttpPost post = null;
CloseableHttpClient client = null;
CloseableHttpResponse resp = null;
try {
client = HttpClients.createDefault();
url = url.replace("ACCESS_TOKEN", AccessTokenKit.getToken());
LOGGER.debug("sending http post to url:[{}]...", url);
post = new HttpPost(url);
post.setEntity(entity);
resp = client.execute(post);
int sc = resp.getStatusLine().getStatusCode();
if (sc >= 200 && sc < 300) {
String respEntity = EntityUtils.toString(resp.getEntity(), Charset.forName("UTF-8"));
LOGGER.debug("success to get response:[{}]", respEntity);
if (resp.getEntity().getContentType().getValue().startsWith("application/json")) {
ErrorEntity err = JSONObject.parseObject(respEntity, ErrorEntity.class);
if (!"0".equals(err.getErrcode()) || !"ok".equals(err.getErrmsg())) {
LOGGER.error("http[{}] response is error, errcode:[{}], errmsg:[{}].", url, err.getErrcode(), err.getErrmsg());
return;
}
}
c.process(respEntity);
LOGGER.debug("success to finish http post request.");
}
} catch (Exception e) {
LOGGER.error("failed to send http post request", e);
} finally {
HttpClientUtils.closeQuietly(resp);
HttpClientUtils.closeQuietly(client);
}
}
Aggregations