okhttp client throwing exception under TMG proxy s

2019-04-17 09:53发布

问题:

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();
    }

}
}

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: 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.

Any help ?

回答1:

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, jesse@swank.ca and I'll confirm this.