gpt4 book ai didi

java - Jax-RS 无法对代理进行身份验证

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:11 24 4
gpt4 key购买 nike

我公司有代理

proxy=myProxy
port=myProxyPort
username=me
password=myPassword

我尝试使用简单的 java.net 函数访问外部世界,它成功了!

System.setProperty("http.proxyHost", myProxy);
System.setProperty("http.proxyPort", myProxyPort);

Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new
PasswordAuthentication(me,myPasssword.toCharArray());
}});

URL u = new URL("http://www.google.com");
HttpURLConnection con = (HttpURLConnection) u.openConnection();
DataInputStream di = new DataInputStream(con.getInputStream());
byte[] b = new byte[1024*512];
while(-1 != di.read(b,0,1024*512)) {
System.out.print(new String(b));

不是我尝试通过使用 Jax-RS Resteasy 实现来做到这一点:

Client client = new ResteasyClientBuilder().defaultProxy(myProxy, myProxyPort).build(); 
System.out.println(client.target("https://www.google.com").request().get().readEntity(String.class));

出现以下错误

Cache Access Denied
ERR_CACHE_ACCESS_DENIED
(squid/3.1.6)
Sorry, you are not currently allowed to request https://www.google.com/* from this cache until you have authenticated yourself

有人可以告诉我如何使用 Jax-RS 使用用户名密码向代理进行身份验证

最佳答案

哇,这个问题真让我抓狂。我不知道如何以独立于实现的方式解决它。不管怎样,现在它是这样工作的:

ResteasyClient client = new ResteasyClientBuilder().defaultProxy(myProxy, myProxyPort).build(); 
Credentials credentials = new UsernamePasswordCredentials(me, mypassword);
ApacheHttpClient4Engine engine = (ApacheHttpClient4Engine)client.httpEngine();
HttpContext context = new BasicHttpContext();
engine.setHttpContext(context);

CredentialsProvider provider = new BasicCredentialsProvider();
context.setAttribute(ClientContext.CREDS_PROVIDER, provider);
AuthScope authScope = new AuthScope(
myProxy,
myProxyPort,
null,
null);
System.out.println(client.target("https://www.google.com").request().get().readEntity(String.class));

关于java - Jax-RS 无法对代理进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30776211/

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