gpt4 book ai didi

spring - RestTemplate postForObject 返回 HttpClientErrorException : 401 null

转载 作者:行者123 更新时间:2023-12-02 20:52:38 25 4
gpt4 key购买 nike

我正在尝试向身份验证服务器发送帖子消息,并且我正在使用 postforobject。当我运行程序时,我收到“线程“main”中的异常 org.springframework.web.client.HttpClientErrorException: 401 null”错误

    RestTemplate rest = new RestTemplate();
String responseEntity;
String uri = "http://...";
responseEntity = rest.postForObject(uri, null, String.class);
System.out.println(responseEntity);

我也尝试过

    responseEntity = rest.postForObject(uri, "", String.class);

同样的结果,它不起作用。

当我使用 Simple Rest Client 时它可以工作 enter image description here

有什么想法吗?

最佳答案

401 null 可能意味着服务器期望某种形式的身份验证,但没有提供任何形式的身份验证(即您的请求中没有授权 header )

最可能的解释是:简单 REST 客户端(重新)使用 Chrome 的 cookie。如果您之前使用表单登录对服务进行了身份验证,则 Simple Rest Client 将重用 JSESSIONID cookie。要检测这一点,只需按 F12 并打开调试器的网络选项卡即可。仅供引用还有其他浏览器插件,例如 "Advanced REST Client" ,这使您可以更清楚地了解发送到服务器的内容。

当您使用restTemplate发送请求时,它无法访问 session cookie(这是预期的)。您应该发布到允许匿名访问的 URI,或者在请求中提供某种形式的身份验证。如果您需要其他帮助,请发布您的安全配置。

关于spring - RestTemplate postForObject 返回 HttpClientErrorException : 401 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42666936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com