Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过HTTPClient在代理后实现HTTP连接方法_Java_Android - Fatal编程技术网

Java 如何通过HTTPClient在代理后实现HTTP连接方法

Java 如何通过HTTPClient在代理后实现HTTP连接方法,java,android,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

我正在使用
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一样建立连接

我的问题是如何通过
HTTPClient
在没有代理的情况下实现
httpconnect
方法。 建立创建连接后,将代理设置为
HTTPClient

  // 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);
我期待着你的答复


谢谢。

我从HTTP隧道维基百科获得了一些信息。客户端要求HTTP代理服务器使用“连接”HTTP方法将TCP连接转发到所需的目标。然后,服务器继续代表客户机建立连接。一旦服务器建立了连接,代理服务器将继续代理与客户端之间的TCP流。请注意,只有最初的连接请求是HTTP—之后,服务器只是代理已建立的TCP连接。我不想通过HTTP代理服务器执行“连接”HTTP方法,可能我的代理服务器无法支持“连接”HTTP方法。所以,您能告诉我如何通过HTTPClient连接主机吗。我知道SSLSocketFactory类可以连接SSL套接字。