OkHTTPClient Proxy 认证如何进行?
OkHTTPClient Proxy authentication how to?
问题:如何给OkHTTP添加授权代理
我知道 OkHTTP 的构建器 does support proxies 虽然我很难设置它。
/**
* Given a Url and a base64 encoded password return the contents of a website.
* @param urlString
* @param password
* @return JSON
*/
public String getURLJson(String urlString, String password) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build();
Request request = new Request.Builder()
.url(urlString)
.get()
.addHeader("authorization", "Basic " + password)
.addHeader("cache-control", "no-cache")
.build();
Response response = null;
try {
response = client.newCall(request).execute();
String string = response.body().string();
response.body().close();
return string;
} catch (IOException e) {
System.err.println("Failed scraping");
e.printStackTrace();
}
return "failed";
}
我有IP/端口/用户名/密码。
虽然我不知道如何将它们变成 Proxy proxy
然后可以在 client.SetProxy() 中使用。
它似乎过于复杂,我似乎无法弄清楚。任何帮助将不胜感激。
试试这个:
int proxyPort = 8080;
String proxyHost = "proxyHost";
final String username = "username";
final String password = "password";
Authenticator proxyAuthenticator = new Authenticator() {
@Override public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic(username, password);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
};
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
.proxyAuthenticator(proxyAuthenticator)
.build();
问题:如何给OkHTTP添加授权代理
我知道 OkHTTP 的构建器 does support proxies 虽然我很难设置它。
/**
* Given a Url and a base64 encoded password return the contents of a website.
* @param urlString
* @param password
* @return JSON
*/
public String getURLJson(String urlString, String password) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build();
Request request = new Request.Builder()
.url(urlString)
.get()
.addHeader("authorization", "Basic " + password)
.addHeader("cache-control", "no-cache")
.build();
Response response = null;
try {
response = client.newCall(request).execute();
String string = response.body().string();
response.body().close();
return string;
} catch (IOException e) {
System.err.println("Failed scraping");
e.printStackTrace();
}
return "failed";
}
我有IP/端口/用户名/密码。
虽然我不知道如何将它们变成 Proxy proxy
然后可以在 client.SetProxy() 中使用。
它似乎过于复杂,我似乎无法弄清楚。任何帮助将不胜感激。
试试这个:
int proxyPort = 8080;
String proxyHost = "proxyHost";
final String username = "username";
final String password = "password";
Authenticator proxyAuthenticator = new Authenticator() {
@Override public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic(username, password);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
};
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
.proxyAuthenticator(proxyAuthenticator)
.build();