情况一:

代码如下:

    public static void main(String[] args) {
		
        //System.setProperty("http.proxyHost", "127.0.0.1");
       // System.setProperty("http.proxyPort", "8888");
        //System.setProperty("https.proxyHost", "127.0.0.1");
       // System.setProperty("https.proxyPort", "8888");
        // 1. 创建 RestTemplate 实例
        RestTemplate restTemplate = new RestTemplate();
//        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());


        // 2. 设置自定义 Header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("cookie", "acw_tc=0a0d09d017411693146552511e23c6ab9abba117901607e49461a42d998b14;xsecappid=ugc;a1=19565c75432or724uwtmow5ji9do8gdlddkg9nusk40000397010;webId=6f1e16b2d5a7f3f0bc172bdb17b73ccf;websectiga=6169c1e84f393779a5f7de7303038f3b47a78e47be716e7bec57ccce17d45f99;sec_poison_id=262875ea-9321-4c02-8e85-06e262bab0e4;gid=yj2K2SW2DdfSyj2K2SW24fI7qJlFWJ47E96lTMTE2V3jfk48lYkf1j888qjW8y88S4q48ffq;customer-sso-sid=68c5174782653724273415240d3ef3b223f735db;x-user-id-creator.xiaohongshu.com=5d8723e9000000000100b8a8;customerClientId=770196689147403;access-token-creator.xiaohongshu.com=customer.creator.AT-68c517478265372427341525uhumdwjwgntyzh7r;galaxy_creator_session_id=Sn3P3hfcxTQZtNYajy0na7gtY416c8UZCJwL;galaxy.creator.beaker.session.id=1741169340356033546881");
        headers.add("origin", "https://creator.xiaohongshu.com");

        // 3. 构建请求体(JSON)

        RequestBodyDemo requestBodyDemo = new RequestBodyDemo();
        List locations = new ArrayList<Map<String,Integer>>();
        Map itemMap = new HashMap<String,Integer>();
        itemMap.put("longitude",0);
        itemMap.put("latitude",0);
        locations.add(itemMap);
        requestBodyDemo.setLocations(locations);
        requestBodyDemo.setKeyword("武汉");
        requestBodyDemo.setPage_num(1);

        // 4. 封装请求实体
        HttpEntity<RequestBodyDemo> requestEntity = new HttpEntity<>(requestBodyDemo, headers);

        // 5. 发送 POST 请求
        ResponseEntity<String> response = restTemplate.exchange(
                "https://www.xiaohongshu.com/web_api/sns/v5/creator/poi/search",
                HttpMethod.POST,
                requestEntity,
                String.class
        );

        // 6. 处理响应
        System.out.println("Status Code: " + response.getStatusCode());
        System.out.println("Response Body: " + response.getBody());
    }

报错如下:
在这里插入图片描述

但是将"restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());"这一行代码注释放开,请求成功如下:
在这里插入图片描述

抓包工具fiddle安装如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
抓包如下:
在这里插入图片描述
在这里插入图片描述

debug源码找原因:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

总结原因如下:
在这里插入图片描述

情况2:

    public static void main(String[] args) throws Exception {

        System.setProperty("http.proxyHost", "127.0.0.1");
        System.setProperty("http.proxyPort", "8888");
        System.setProperty("https.proxyHost", "127.0.0.1");
        System.setProperty("https.proxyPort", "8888");
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Cookie","did=web_8c2427842437cd3bb981ed728c184823; didv=1721093381095; soft_did=1619580708547; bUserId=1000413663015; userId=2123355009; kuaishou.web.cp.api_st=ChZrdWFpc2hvdS53ZWIuY3AuYXBpLnN0ErABfkEQ8w1xIjhi0FaPhU5ltiOQMsrRJ8iaNYF9Ry_og-y-9FSVhH8rq7eXApZD00UOz3Oom5LyFTUpM0w6UM0_IUAEIWSgmfhxTqBUmWcmRq4u1nC2bqhQdm9fTQrZeFcUCMzv4GRv_iAAQaY7kSr9Gh-4lxSDzOJc8Xts_1MSWeb7pLJLFeidkMkZEdeMz6wrCnXg5AwEJxaWiYBX_39-jBlXO__8fdKlfhYhqXk1qQwaEmyq_OYDEGWlx5BhBFmCK8sWsSIgxaoo3DpNtuLA4eqUisavQa7AO5cDxpVRzvCHSqQf5ywoBTAB; kuaishou.web.cp.api_ph=798b51a4e4e069cd60b6350943e57647da22");
        httpHeaders.add("Content-Type","application/json");
        Map<String, Object> body = new HashMap<>();
        body.put("cityName", "武汉市");
        body.put("count", 20);
        body.put("keyword",  "武汉市");
        body.put("pcursor", "0$0.000000,0.000000");
        HttpEntity< Map<String, Object>> httpEntity = new HttpEntity<>(body, httpHeaders);
        ResponseEntity<String> exchange = restTemplate.postForEntity("https://cp.kuaishou.com/rest/zt/location/wi/poi/search?kpn=kuaishou_cp&subBiz=CP%2FCREATOR_PLATFORM&kpf=PC_WEB",httpEntity,String.class);
        if(exchange.getStatusCode().is2xxSuccessful()){

        }

//        OkHttpClient client = new OkHttpClient();
////                client.setProxy(proxy);
//        Request.Builder builder = new Request.Builder();
//        builder.addHeader("cookie","did=web_8c2427842437cd3bb981ed728c184823; didv=1721093381095; soft_did=1619580708547; bUserId=1000413663015; userId=2123355009; kuaishou.web.cp.api_st=ChZrdWFpc2hvdS53ZWIuY3AuYXBpLnN0ErABfkEQ8w1xIjhi0FaPhU5ltiOQMsrRJ8iaNYF9Ry_og-y-9FSVhH8rq7eXApZD00UOz3Oom5LyFTUpM0w6UM0_IUAEIWSgmfhxTqBUmWcmRq4u1nC2bqhQdm9fTQrZeFcUCMzv4GRv_iAAQaY7kSr9Gh-4lxSDzOJc8Xts_1MSWeb7pLJLFeidkMkZEdeMz6wrCnXg5AwEJxaWiYBX_39-jBlXO__8fdKlfhYhqXk1qQwaEmyq_OYDEGWlx5BhBFmCK8sWsSIgxaoo3DpNtuLA4eqUisavQa7AO5cDxpVRzvCHSqQf5ywoBTAB; kuaishou.web.cp.api_ph=798b51a4e4e069cd60b6350943e57647da22");
//        builder.addHeader("Content-Type","application/json");
//        Map<String,Object> body=new HashMap<>();
//        body.put("cityName","武汉市");
//        body.put("count",20);
//        body.put("keyword","武汉市");
//        body.put("pcursor","0$0.000000,0.000000");
//        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), JSON.toJSONString(body));
//        Request request = builder.url("https://cp.kuaishou.com/rest/zt/location/wi/poi/search?kpn=kuaishou_cp&subBiz=CP%2FCREATOR_PLATFORM&kpf=PC_WEB").post(requestBody).build();
//        Response httpResponse = client.newCall(request).execute();
//        System.out.println("111");
    }

使用resttemplate请求第三方接口报错401,改用okhttp4请求第三方接口正常!!

在这里插入图片描述

在这里插入图片描述
因为代码里面的url已经是编码过的url,这里resttemplate又对url进行了2次编码。导致错误!!!
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

更多推荐