收藏
回答

java后端请求httpClient发送http请求,返回412状态码

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug 登录 微信安卓客户端 6.5.3 2.0.0

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也是没有办法发出请求

最后一次编辑于  2020-05-13
回答关注问题邀请回答
收藏

1 个回答

  • 启年网络
    启年网络
    2020-05-13

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


    2020-05-13
    有用
    回复
登录 后发表内容
问题标签