2016-12-05 20:23:30.303 INFO 3392 --- [pool-2-thread-1] com.wx.aibox.service.token.TokenAuth : >Update token16-12-5 下午8:23 2016-12-05 20:23:30.304 INFO 3392 --- [pool-2-thread-1] com.wx.aibox.service.token.TokenService : https%3A%2F%2Fapi.weixin.qq.com%2Fcgi-bin%2Ftoken%3Fgrant_type%3Dclient_credential%26appid%3Dwx6f41feeb796d964e%26secret%3D0f8aab5aac94d181caa0d0cfa94b7fe6+ 2016-12-05 20:23:30.312 INFO 3392 --- [pool-2-thread-1] com.wx.aibox.tools.HttpClient : httpGet: https%3A%2F%2Fapi.weixin.qq.com%2Fcgi-bin%2Ftoken%3Fgrant_type%3Dclient_credential%26appid%3Dwx6f41feeb796d964e%26secret%3D0f8aab5aac94d181caa0d0cfa94b7fe6+ 2016-12-05 20:23:30.444 INFO 3392 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 80 (http) 2016-12-05 20:23:30.456 INFO 3392 --- [ main] com.wx.aibox.WxApplication : Started WxApplication in 13.379 seconds (JVM running for 15.142) org.apache.http.client.ClientProtocolException at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
package com.wx.aibox.service.token; */ public void getToken() throws UnsupportedEncodingException { String url=Constant.url; url=URLEncoder.encode(url, "UTF-8"); try {logger.info(""+url); JSONObject jsonObject = HttpClient.httpGet(url); logger.info(" json"+jsonObject.toString()); tokenAuthEachRsp = (TokenAuthEachRsp) jsonObject.toJavaObject(TokenAuthEachRsp.class); logger.info("成功"+tokenAuthEachRsp.toString()); }catch (Exception e){ logger.error(e.toString()); try{Thread.sleep(2000);}catch (InterruptedException r){r.printStackTrace();} logger.info("token 失败 重新发送"); // getToken(); } } import java.io.IOException; import java.net.URLDecoder; import static org.apache.commons.httpclient.methods.multipart.FilePart.DEFAULT_CHARSET; import static org.springframework.http.HttpHeaders.ACCEPT; import static org.springframework.http.HttpHeaders.HOST; public class HttpClient { private static final Logger logger = LoggerFactory.getLogger(HttpClient.class); /** * 发送get请求 * @param url 路径 * @return */ public static JSONObject httpGet(String url){ logger.info(" httpGet: {}",url); JSONObject jsonResult = null; try { CloseableHttpClient client= HttpClients.createDefault(); HttpGet request = new HttpGet(url); HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(url); request.addHeader("Accept-Charset", "UTF-8"); request.addHeader("Host", HOST); request.addHeader("Accept", ACCEPT); HttpResponse response = client.execute(request); logger.info(" response.getStatusLine().getStatusCode(){}",response.getStatusLine().getStatusCode()); /**请求发送成功,并得到响应**/ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String strResult = EntityUtils.toString(response.getEntity()); logger.info(" json 返回值{}",response.toString()); logger.info(" json 返回值{}",strResult); jsonResult = JSONObject.parseObject(strResult); url = URLDecoder.decode(url, "UTF-8"); } else { logger.error("get请求提交失败:" + url+"code"+response.getStatusLine().getStatusCode()); jsonResult= JSONObject.parseObject(String.valueOf(response.getStatusLine().getStatusCode())); } logger.info("HttpStatus {}",response.getStatusLine().getStatusCode() ); } catch (Exception e) { e.printStackTrace(); logger.error("get请求提交失败:" + url, e); } logger.error("result" + jsonResult); logger.error("result" + jsonResult.toString()); return jsonResult; } }
相关分类