gpt4 book ai didi

java - 尝试用 Java 发送请求时出现错误 407

转载 作者:行者123 更新时间:2023-12-01 09:07:59 25 4
gpt4 key购买 nike

我有以下代码:

public class SendRequest {

public static void main(String[] args) throws ClientProtocolException, IOException {

String url = "http://backoffice.xyz";

HttpHost proxy = new HttpHost("proxy.proxy", 8080, "http");
HttpClient client = HttpClientBuilder.create().setProxy(proxy).build();
HttpGet request = new HttpGet(url);

//request.addHeader("User-Agent", "USER-AGENT");
HttpResponse response = client.execute(request);

System.out.println("Response Code: " + response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while((line = rd.readLine()) != null){
result.append(line);
}
System.out.println(result.toString());

}

}

这将返回 407 未经授权的访问/缓存访问被拒绝错误。我需要包含什么代码才能通过代理进行身份验证?

最佳答案

您的代理是否需要基于用户名/密码的身份验证?如果是这样,请尝试实现 java.net.Authenticator。我想你需要设置 useSystemProperties

Authenticator.setDefault(new Authenticator(){
密码验证 getPasswordAuthentication(){
返回新的PasswordAuthentication(用户名,密码);
}
});

您可能需要将 setDefaultCredentialsProvider(CredentialsProvidercredentialsProvider) 添加到 HTTPClientBuilder 并使用 SystemDefaultCredentialsProvider 实例。

关于java - 尝试用 Java 发送请求时出现错误 407,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41099529/

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