okhttp客户端在TMG代理服务器下抛出异常

时间:2014-11-17 10:33:45

标签: java https proxy okhttp

我正在为我的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();
    }

}
}

我的网络连接位于需要代理用户身份验证的Microsoft TMG代理服务器后面,执行上述代码时出现以下异常:

java.net.ProtocolException:意外的状态行:< HTML>< HEAD>< TITLE>错误消息< / TITLE>         在com.squareup.okhttp.internal.http.StatusLine.parse(StatusLine.java:54)         在com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187)         在com.squareup.okhttp.Connection.makeTunnel(Connection.java:395)         在com.squareup.okhttp.Connection.upgradeToTls(Connection.java:223)         在com.squareup.okhttp.Connection.connect(Connection.java:153)         在com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:169)         在com.squareup.okhttp.OkHttpClient $ 1.connectAndSetOwner(OkHttpClient.java:119)         at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:134)         在com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:314)         在com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:237)         在com.squareup.okhttp.Call.getResponse(Call.java:233)         在com.squareup.okhttp.Call.execute(Call.java:84)

我尝试在不同的代理服务器下工作,工作得很好。

有任何帮助吗?

1 个答案:

答案 0 :(得分:0)

看起来好像是OkHttp中的一个错误,我们在407上没有丢弃完整的响应主体。在我们的GitHub问题跟踪器上打开一个问题&我会看一看。如果公共互联网可以访问感兴趣的代理,请私下发送给我jesse@swank.ca,我会确认这一点。