gpt4 book ai didi

java - 通过 web 代理使用 restful webservice

转载 作者:搜寻专家 更新时间:2023-11-01 02:55:08 25 4
gpt4 key购买 nike

我正在尝试使用 Apache Wink framework 在 Java 中使用一个 Restful Web 服务通过需要身份验证的我的学校网络代理

ClientConfig clientConfig = new ClientConfig();
clientConfig.proxyHost("proxy.school.com");
clientConfig.proxyPort(3128);
//nothing to set username and password :(

RestClient client = new RestClient(clientConfig);
Resource resource = client.resource("http://vimeo.com/api/v2/artist/videos.xml");
String response = resource.accept("text/plain").get(String.class);

我也尝试过使用 BasicAuthSecurityHandler 但它似乎被用来直接向网络服务器进行身份验证,而不是网络代理

BasicAuthSecurityHandler basicAuthHandler = new BasicAuthSecurityHandler();
basicAuthHandler.setUserName("username");
basicAuthHandler.setPassword("password");
config.handlers(basicAuthHandler);

它仍然失败并显示 HTTP 407 错误代码:需要代理身份验证。

我已经尽我所能地进行了谷歌搜索,没有什么比通过 Web 代理从 Java 客户端使用 Web 服务更好的了,如果有人有其他想法,请随时回复

最佳答案

好吧,这很难,但我找到了!我用 Fiddler 记录了从我的浏览器发出的 HTTP 请求。并发现 Proxy-ConnectionProxy-Authorization 是我在阅读大量文档(如 RFC 2616)后寻找的内容关于 HTTP/1.1

所以我将发送的值复制粘贴到我的 java 代码中:

resource.header("Proxy-Connection", "Keep-Alive");
resource.header("Proxy-Authorization", "Basic encodedString");

其中 encodedString 是我的浏览器发送的内容:username:password base64 encoded

现在它可以完美运行了:)

关于java - 通过 web 代理使用 restful webservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3023885/

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