问题描述
我正在为我的java应用程序使用github的方形OKHTTP客户端库,如下所示:
I'm using github's square OKHTTP client library for my java application as follows :
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.squareup.okhttp.Authenticator;
import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
public class TestOkHttp {
static public void main(String argcp[]){
try {
OkHttpClient okHttpClient = new OkHttpClient();
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
}
}};
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
okHttpClient.setSslSocketFactory(sc.getSocketFactory());
Proxy p=new Proxy(Proxy.Type.HTTP,new InetSocketAddress("10.1.1.0", 8080));
okHttpClient.setProxy(p);
HostnameVerifier hostNameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String sourceHost, SSLSession arg1) {
// TODO Auto-generated method stub
return sourceHost.equals("serverwithtls.com");
}
};
okHttpClient.setHostnameVerifier(hostNameVerifier);
okHttpClient.setAuthenticator(new Authenticator() {
@Override
public Request authenticateProxy(Proxy proxy, Response response)
throws IOException {
String credential = Credentials.basic("username","password");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
@Override
public Request authenticate(Proxy arg0, Response arg1)
throws IOException {
// TODO Auto-generated method stub
return null;
}
});
Request request = new Request.Builder()
.url("https://serverwithtls.com")
.build();
Response response = okHttpClient.newCall(request).execute();
System.out.println(response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的网络连接是Microsoft TMG代理服务器后面需要代理用户身份验证,执行上述代码时出现以下异常:
My network connection is behind a Microsoft TMG proxy server which requires proxy user authentication, I get the following exception upon execution of the above code :
java。 net.ProtocolException:意外的状态行:< HTML>< HEAD>< TITLE>错误消息< / TITLE>
at com.squareup.okhttp.internal.http.StatusLine.parse(StatusLine.java:54)
at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187)
at com.squareup.okhttp.Connection.makeTunnel(Connection.java:395)
at com.squareup.okhttp.Connection.upgradeToTls(Connection.java:223)
at com.squareup .okhttp.Connection.connect(Connection.java:153)
at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:169)
at com.squareup.okhttp.OkHttpClient $ 1.connectAndSetOwner( OkHttpClient.java:119)
at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:134)
at com.squareup.okhttp.internal.http.HttpEngine.connect( HttpEngine.java:314)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:237)
at com.squareup.okhttp.Call.getResponse(Call.java: 233)
at com.squareup.okhttp.Call.execute( Call.java:84)
java.net.ProtocolException: Unexpected status line: <HTML><HEAD><TITLE>Error Message</TITLE> at com.squareup.okhttp.internal.http.StatusLine.parse(StatusLine.java:54) at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187) at com.squareup.okhttp.Connection.makeTunnel(Connection.java:395) at com.squareup.okhttp.Connection.upgradeToTls(Connection.java:223) at com.squareup.okhttp.Connection.connect(Connection.java:153) at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:169) at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:119) at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:134) at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:314) at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:237) at com.squareup.okhttp.Call.getResponse(Call.java:233) at com.squareup.okhttp.Call.execute(Call.java:84)
我尝试在不同的代理服务器下工作,工作得很好。
I tried working under different proxy servers, worked perfectly.
任何帮助?
推荐答案
看起来好像是OkHttp中的一个错误,我们在那里没有扔掉407上的完整响应体。打开一个我们的GitHub问题跟踪器问题&我会看一看。如果公共互联网可以访问感兴趣的代理,请私下发送给我, [email protected]
我会确认这一点。
Looks like a bug in OkHttp, where were we're not throwing away the full response body on a 407. Open an issue on our GitHub issue tracker & I'll take a look. If the proxy of interest is reachable by the public Internet, send it to me privately, [email protected]
and I'll confirm this.
这篇关于okhttp客户端在TMG代理服务器下抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!