通过Web代理消费Restful Web服务

4

我正在尝试使用Apache Wink框架在Java中消费一个RESTful webservice,需要通过我的学校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,但似乎它被用于直接对Web服务器进行身份验证,而不是Web代理。

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

仍然以HTTP 407错误代码失败:需要代理身份验证。

我已经尽力搜索了最佳方法,但没有更好的方法能够通过Web代理从Java客户端消费Web服务。如果有其他想法,请随意回复。

2个回答

3

好的,这有点困难,但我找到了!我使用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编码后的字符串。

现在它已经完美运行了:)


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接