java后端请求httpClient发送http请求,返回412状态码
发布于 5 年前 作者 qiang78 1027 次浏览 来自 官方Issues

https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/backend-api.html

String appId = "";
        String appSerc = "";
        String jsCode = "";//登录时的code
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + appSerc + "&js_code=" + jsCode + "&grant_type=authorization_code";
        System.out.println(url);
        HttpGet httpGet = new HttpGet(url);
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        try (
                CloseableHttpClient client = httpClientBuilder.build();
                CloseableHttpResponse response = client.execute(httpGet);
        ){
            log.info("状态码:"+response.getStatusLine().getStatusCode(),this);
            System.out.println("返回结果:" + JSON.toJSONString(response.getEntity()));
        }catch (Exception e){
            e.printStackTrace();
        }
输出:
https://api.weixin.qq.com/sns/jscode2session?appid=&secret=&js_code=&grant_type=authorization_code

状态码:200
{"chunked":false,"content":{},"contentLength":78,"contentType":{"buffer":{"empty":false,"full":false},"elements":[{"name":"text/plain","parameterCount":0,"parameters":[]}],"name":"Content-Type","value":"text/plain","valuePos":13},"repeatable":false,"streaming":true}


这里把id和code去掉了,如果加上,httpClient也是没有办法发出请求

1 回复

EntityUtils.toString(entity, "UTF-8");

回到顶部