okhttp客户端在TMG代理服务器下抛出exception
我正在为我的java应用程序使用github的方形OKHTTP客户端库,如下所示:
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(); } } }
我的网络连接位于需要代理用户身份validation的Microsoft TMG代理服务器后面,执行上述代码时出现以下exception:
java.net.ProtocolException:意外的状态行:com.squareup的com.squareup.okhttp.internal.http.StatusLine.parse(StatusLine.java:54)中的
我尝试在不同的代理服务器下工作,工作得很好。
有帮助吗?
看起来好像是OkHttp中的一个错误,我们在407上没有扔掉完整的响应主体。在我们的GitHub问题跟踪器上打开一个问题我会看一看。 如果公共互联网可以访问感兴趣的代理,请私下发送给我, jesse@swank.ca
,我会确认这一点。
- 使用Java https错误上传到Imgur v3
- 将“jsse.enableSNIExtension”设置为false会带来什么安全后果?
- 什么可能导致套接字ConnectException:连接超时?
- 禁用单个连接的SSLHandshakeException
- 通过HTTPS / SSL通过自己的Java客户端连接的问题
- 如何使用com.sun.net.httpserver.HttpsServer要求客户端证书
- SSLHandshakeException:收到致命警报:在Java 6 – > 8升级后的handshake_failure
- 具有自签名证书的Java SSL连接,无需将完整密钥库复制到客户端
- HTTP / 1.1 302暂时移动 – 在Android API 16-17上发生