Get token

Copyright: respect for the original, as to reproduction, please write source. Thank https://blog.csdn.net/qq_39052982/article/details/89477015

Let's say today is how to get the token by the code
obtained token, then First Party will provide some data to you

Here Insert Picture Description
Like this, token into and out of the reference parameters.

Then you can write code.

/**
	 * 获取token
	 * @return
	 */
	public String getToken(){
		String shortUrl = WEIXIN_SHORT_TOKEN;	//这是一个token的入参url 只是我把它放到配置文件了。
		logger.info("json_token_short requestUrl:"+ shortUrl);
        String jsonTokenShort = null;
		try {
			jsonTokenShort = httpClientWraper.mockPostJsonNew(shortUrl, null, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
        logger.info("获取jsonTokenShort响应:"+jsonTokenShort);
        Map<String,Object> mapResultShort = (Map)JSON.parse(jsonTokenShort);
//        if (jsonTokenShort.contains("errcode") && !mapResultShort.get("errcode").toString().equals("0")) {
//            throw new GzhBizRuntimeException(mapResultShort.get("errcode").toString(),mapResultShort.get("errmsg").toString());
//        }
        Object tokenShortA = mapResultShort.get("value");
        if(tokenShortA == null){
        	throw new GzhBizRuntimeException(RespCodeEnum.GET_SHORT_TOKEN_ERREO.getRespCode(),
                    RespCodeEnum.GET_SHORT_TOKEN_ERREO.getRespMsg());
        }
        String tokenShortB = mapResultShort.get("value").toString();
        String longUrl = WEIXIN_LONG_TOKEN.concat("dataface/wechatKey?token=") + tokenShortB ;
        logger.info("json_token_long requestUrl:"+ longUrl);
//        String longUrl = String.format(url_template,tokenShort);
//        logger.info("json_token_long requestUrl:"+ longUrl);
        String jsonTokenLong = null;
		try {
			jsonTokenLong = httpClientWraper.mockPostJsonNew(longUrl, null, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
        logger.info("获取jsonTokenShort响应:"+ jsonTokenLong);
        Map<String,Object> mapResultLong = (Map)JSON.parse(jsonTokenLong);
//        if (jsonTokenShort.contains("errcode") && !mapResultLong.get("errcode").toString().equals("0")) {
//            throw new GzhBizRuntimeException(mapResultLong.get("errcode").toString(),mapResultLong.get("errmsg").toString());
//        }
        Object tokenLongA = mapResultLong.get("value");
        if(tokenLongA == null){
        	throw new GzhBizRuntimeException(RespCodeEnum.GET_LONG_TOKEN_ERREO.getRespCode(),
                    RespCodeEnum.GET_LONG_TOKEN_ERREO.getRespMsg());
        }
        String tokenLongB = mapResultLong.get("value").toString();	
        Map<String,Object> mapResult = (Map)JSON.parse(tokenLongB);
        String token = mapResult.get("key").toString();
        tokenMap.put("token", token);
        logger.info("token>>>>>>>>>>>>>>>>>>>>>>>" + token);
        return token;
	}

This method can be used to get a token.
This is to obtain twice, first to obtain a short token, then a second time to obtain and use the token length of the splicing short spliced token also go up. And finally get to the end of the token. Get twice for safety.

There are tools which have submitted a method of submitting a post. I can only be accessed by reference into this post.

  public String mockPostJsonNew(String url,String parameter,boolean isValid) throws Exception {
		PostMethod postMethod = new PostMethod(url);
		RequestEntity requestEntity;
		if(parameter != null){
			try {
				requestEntity = new StringRequestEntity(parameter,"application/json","UTF-8");
			} catch (UnsupportedEncodingException e) {
				logger.error(e.getMessage());
				throw new Exception(e.getMessage());
			}  
			postMethod.setRequestEntity(requestEntity); 
		}
		return executeMethod(postMethod,isValid);
	}

If it is, then I also get to submit written submissions get a method

public  String mockGet(String url) {
        HttpClient httpClient = new HttpClient();
        if(isValid){
            logger.info(">>>>>>>>>>>>>>>开启代理。");
            //设置代理请求
            httpClient.getHostConfiguration().setProxy(proxyAddress,proxyPort);
        } else {
            logger.info(">>>>>>>>>>>>>>>未开启网络代理。");
        }
        HttpMethod getMethod = new GetMethod(url);
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            if (HttpURLConnection.HTTP_OK == statusCode) {
                return execute(getMethod);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            throw new GzhBizRuntimeException("-1", e.getMessage(),e);
        }

        return "";
    }
    

If you have not set up a proxy, then this agent can be deleted.

Finally ...
Bloggers is a white, work is currently on record in terms of bloggers more difficult problem.
If an error please private letter bloggers. Thank you, do not understand you can also find bloggers oh ~

Guess you like

Origin blog.csdn.net/qq_39052982/article/details/89477015