【发布时间】:2017-08-11 11:59:42
【问题描述】:
我有这个 HTTP 隧道的代码
private static void doTunnelHandshake(Socket conn, String proxyHost, int proxyPort, String host, int port) throws IOException {
String msg = "CONNECT " + host + ":" + port + " HTTP/1.1\n" + "Host: https://" + proxyHost + ":" + proxyPort + "\r\n" + "User-Agent: " + sun.net.www.protocol.http.HttpURLConnection.userAgent+ "\r\n\r\n";
OutputStream out = conn.getOutputStream();
byte b[];
try {
/*
* We really do want ASCII7 -- the http protocol doesn't change with
* locale.
*/
b = msg.getBytes("ASCII7");
} catch (UnsupportedEncodingException ignored) {
/*
* If ASCII7 isn't there, something serious is wrong, but Paranoia
* Is Good (tm)
*/
b = msg.getBytes();
}
out.write(b);
out.flush();
/*
* We need to store the reply so we can create a detailed error message
* to the user.
*/
byte reply[] = new byte[200];
int replyLen = 0;
int newlinesSeen = 0;
boolean headerDone = false; /* Done on first newline */
InputStream in = conn.getInputStream();
while (newlinesSeen < 2) {
int i = in.read();
if (i < 0) {
throw new IOException("Unexpected EOF from proxy");
}
if (i == '\n') {
headerDone = true;
++newlinesSeen;
} else if (i != '\r') {
newlinesSeen = 0;
if (!headerDone && replyLen < reply.length) {
reply[replyLen++] = (byte) i;
}
}
}
/*
* Converting the byte array to a string is slightly wasteful in the
* case where the connection was successful, but it's insignificant
* compared to the network overhead.
*/
String replyStr;
try {
replyStr = new String(reply, 0, replyLen, "ASCII7");
} catch (UnsupportedEncodingException ignored) {
replyStr = new String(reply, 0, replyLen);
}
/* We asked for HTTP/1.0, so we should get that back */
if (!replyStr.startsWith("HTTP/1.0 200")) {
throw new IOException("Unable to tunnel for " + host + ":" + port + ". TunnelProxy returns \"" + replyStr + "\"");
}
/* tunneling Handshake was successful! */
}
但 apache 回复
“HTTP/1.1 400 错误请求”
【问题讨论】: