如何通过代理后面的HTTPClient进行HTTP CONNECT方法

时间:2011-05-04 00:43:38

标签: java android

我正在使用HTTPClient对浏览器进行编程,以便在Android上访问HTTPS。 以下源代码已通过HTTPClient实现了访问HTTPS。

  // Proxy Host and Port
  String proxyHost = "127.0.0.1";
  int proxyPort = 10000;
  // HTTPS Site
  String strURL_https = "https://172.17.4.37:8443/apache.html"; 
  URL url_https = null;
  HttpClient httpClient = null;
  HttpGet get = null;
  try{
    url_https = new URL(strURL_https);
    MySSLSocketFactory sf = MySSLSocketFactory.getSocketFactory();              
    get = new HttpGet(strURL_https);
    try {

      HttpParams params = new BasicHttpParams();
      HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
      HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

      // Add two scheme for Port 443 and 8443        
      httpClient = new DefaultHttpClient();
      Scheme sch = new Scheme("https", sf, 443);
      Scheme sch2 = new Scheme("https", sf, 8443);
      httpClient.getConnectionManager().getSchemeRegistry().register(sch);
      httpClient.getConnectionManager().getSchemeRegistry().register(sch2);
      // Set Proxy   
      httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, new HttpHost(proxyHost, proxyPort));

   } catch (Exception e) {
     e.printStackTrace();           
   }
   HttpContext context = new BasicHttpContext();
   // Execute GET Method
   HttpResponse response = httpClient.execute(get, context);
   HttpEntity entity = response.getEntity();
   String charSet = null;
   String contentType = null;
   StringBuffer sb = null;
   if(entity!= null){
     charSet=EntityUtils.getContentCharSet(entity);
     contentType=entity.getContentType().getValue();
     InputStream is = entity.getContent();
     BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf8"));
     String text = null;
     String line = br.readLine();
     sb = new StringBuffer();
     while(line != null) {
       sb.append(line+"\r\n");
       line = br.readLine();
     }
   }
   // Load Data on Webview
   webview.loadDataWithBaseURL(strURL_https, sb.toString(), "text/html", charSet, null);

但是当httpClient.execute(get, context)时,我的代理服务器将接收HTTP CONNECT方法,如下所示:

CONNECT 172.17.4.37:8443 HTTP/1.1
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
Host: 172.17.4.37:8443

我的代理服务器将HTTP方法发送到VPN,但无法获得任何响应。 在我看来,我应该像HTTP/1.0 200 Connection Established

我的问题是如何HTTP CONNECT方法HTTPClient没有代理。 建立连接后,将代理设置为HTTPClient

我期待你的回答。

谢谢。

0 个答案:

没有答案