编程语言
首页 > 编程语言> > Java-Jax-RS无法验证代理

Java-Jax-RS无法验证代理

作者:互联网

我公司有代理

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));

标签:jax-rs,resteasy,squid,java
来源: https://codeday.me/bug/20191120/2041970.html