与 Android 的 HTTP CONNECT 方法

问题描述 投票:0回答:1

我想扩展一个现有的 Android 应用程序,该应用程序建立到远程设备的 http 连接,并向该设备发送命令并从中接收值。

该功能包括通过已设置的自定义代理服务器进行隧道连接。我给出了 http 标头格式,这应该使代理服务器为我的应用程序创建并提供隧道。

CONNECT <some id>.<target ip>:80 HTTP/1.1
Host: <proxy ip>:443
Authorization: basic <base64 encoded auth string>

# Here beginns the payload for the target device. It could be whatever.
GET / HTTP/1.1
Host: 127.0.0.1:80

该应用程序使用 Apache HttpClient 库来处理其连接,我想与之集成。但这不是强制性的。
授权是标准符合基本授权。

我在实现这一点时遇到了麻烦,因为我不清楚

HttpClient
是如何用于这种行为的。 库中没有
CONNECT
方法,只有
GET
POST
等。我认为这将由
HttpClient
实例的代理设置进行管理。
这里的问题是请求行不是标准的,因为
CONNECT
行包含一个 id,然后自定义代理将解析和解释该 id。

我现在想知道是否有任何预期的方法可以使用 Apache HttpClient 来实现此目的,以及给出的示例数据会是什么样子,或者我是否必须为此实现我自己的方法。如果是这样,它应该实现哪个接口(有一些听起来合理的继承)。

任何解释、片段或指针将不胜感激。

更新: 我现在已经设置了一个小片段,没有 Android。只是普通的 Java 和 Apache HttpClient。我仍然认为请求中的

Host
不匹配是一个问题,因为我无法建立连接。

final HttpClient httpClient = new DefaultHttpClient();

// Set proxy
HttpHost proxy = new HttpHost (deviceId + "." + "proxy ip", 443, "https");
httpClient.getParams().setParameter (ConnRoutePNames.DEFAULT_PROXY, proxy);

final HttpGet httpGet = new HttpGet("http://" + "target device ip");
httpGet.addHeader ("Authorization", "Basic" + 
    Base64.encodeBase64String((username + ":" + password).getBytes()));
// Trying to overvrite the host in the header containing the device Id
httpGet.setHeader("Host", "proxy ip");

System.out.println("Sending request..");
try {
    final HttpResponse httpResponse = httpClient.execute (httpGet);
    final InputStream inputStream = httpResponse.getEntity ().getContent ();
    final InputStreamReader inputStreamReader = 
        new InputStreamReader(inputStream, "ISO-8859-1");

    final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    final StringBuilder stringBuilder = new StringBuilder ();
    String bufferedStrChunk = null;

    while ((bufferedStrChunk = bufferedReader.readLine ()) != null) {
        stringBuilder.append (bufferedStrChunk);
    }

    System.out.println("Received String: " + stringBuilder.toString());
}
catch (final ClientProtocolException exception) {
    System.out.println("ClientProtocolException");
    exception.printStackTrace();
}
catch (final IOException exception) {
    System.out.println("IOException");
    exception.
}

从“它实际上可以工作”的角度来看,这对我来说看起来相当不错。 无论如何,我收到以下日志和跟踪:

Sending request..
2015/03/03 13:16:16:199 CET [DEBUG] ClientParamsStack - 'http.route.default-proxy': https://"device id"."proxy ip":443
2015/03/03 13:16:16:207 CET [DEBUG] SingleClientConnManager - Get connection for route HttpRoute[{}->https://"device id"."proxy ip":443->http://"target device ip"]
2015/03/03 13:16:16:549 CET [DEBUG] ClientParamsStack - 'http.tcp.nodelay': true
2015/03/03 13:16:16:549 CET [DEBUG] ClientParamsStack - 'http.socket.buffer-size': 8192
2015/03/03 13:16:16:563 CET [DEBUG] DefaultClientConnection - Connection shut down
2015/03/03 13:16:16:563 CET [DEBUG] SingleClientConnManager - Releasing connection org.apache.http.impl.conn.SingleClientConnManager$ConnAdapter@bc6a08
Exception in thread "main" java.lang.IllegalStateException: Unable to establish route.
planned = HttpRoute[{}->https://"device id"."proxy ip":443->http://"target device ip"]
current = HttpRoute[{s}->https://"device id"."proxy ip":443->http://"target device ip"]
    at org.apache.http.impl.client.DefaultRequestDirector.establishRoute(DefaultRequestDirector.java:672)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:385)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
    at run.main(run.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

有什么想法出了什么问题吗?

更新:如here所述,这可能是由于重定向时的错误造成的。目标确实重定向的事实告诉我,我没有达到正确的目标,这意味着

Host
参数可能尚未被覆盖。

android http proxy httpclient
1个回答
1
投票

事实上,这根本无法用

HttpClient
来完成,我在错误的级别上尝试过。当使用
TcpSocket
(或
SSLSocket
)完成时,它会起作用。自定义
CONNECT
标头可以简单地组装并发送,如下所示:

final Socket tcpSocket = SSLSocketFactory.getDefault().createSocket("host ip", 443);
String connect = "custom CONNECT header";

tcpSocket.getOutputStream().write((connect).getBytes());
tcpSocket.getOutputStream().flush();

然后可以使用

BufferedReader
或其他方式读取来自服务器的响应。

© www.soinside.com 2019 - 2024. All rights reserved.