我尝试创建原始http请求:
GET /samplepath HTTP/1.1
Cache-Control: max-age=0
Host: sub.sample.net
Date: Fr., 20 Jan. 2017 12:45:39 GMT
DNT: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*\/*;q=0.8
Connection: close
Upgrade-Insecure-Requests: 1
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
我得到的是:
HTTP/1.1 301 Moved Permanently
Location: https://sub.sample.net/samplepath/
Transfer-Encoding: chunked
X-Lima-Id: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
Content-Type: text/html; charset=iso-8859-1
Date: Fri, 20 Jan 2017 11:45:40 GMT
Server: openresty
Set-Cookie: _lcp=a; Path=/; expires=Mon Mar 20 2034 13:02:58
Connection: close
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://sub.sample.net/samplepath/">here</a>.</p>
<hr>
<address>Apache Server at sub.sample.net Port 443</address>
<script type="text/javascript">window.setTimeout(function() { document.cookie = "_lcp3=a; Path=/; expires=Mon Mar 20 2034 13:02:58" }, 1000);</script></body></html>
我尝试通过HttpsURLConnection上的SSLSocket发送请求:
Socket socket = HttpsURLConnection.getDefaultSSLSocketFactory().createSocket(url.getHost(), url.getPort() == -1 ? 443 : url.getPort());
StringBuilder stringBuilder = new StringBuilder("GET /samplepath HTTP/1.1");
stringBuilder.append("\r\n");
stringBuilder.append("Cache-Control: max-age=0\r\n");
...
stringBuilder.append("Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4\r\n");
DataOutputStream wr = new DataOutputStream(socket.getOutputStream());
wr.write(stringBuilder.toString().getBytes());
wr.close();
InputStream is = socket.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytesBuffer = new byte[1];
int offset = 0;
while (inputStream.read(bytesBuffer) != -1) {
byteArrayOutputStream.write(bytesBuffer);
}
byte[] respBytes = byteArrayOutputStream.toByteArray();
System.out.println(new String(respBytes));
我还尝试从默认SSLFactory获取SSLSocket并执行startHandshake()相同的结果。
socket = SSLSocketFactory.getDefault().createSocket(url.getHost(), url.getPort() == -1 ? 443 : url.getPort());
((SSLSocket) socket).startHandshake();
问题是我被重定向到我请求的网站的网站。如果我尝试通过浏览器请求。没有重定向。即使我尝试执行重定向,我又重新定向了一次。
那我在这里做错了什么?
它是SSLSocket的初始化吗?
是从SSLSocket到Socket的变化吗?
我是否错过了TLS的内容?
或者这是一个我错过的完整的其他问题?
我试着在上个月完成这个,但没有解决方案。
修改
我也是这样尝试的。
GET /samplepath HTTP/1.1
Cache-Control: max-age=0
Host: XX.XX.XX.XXX:XXXXX
Date: Fr., 27 Jan. 2017 22:50:39 GMT
DNT: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*\/*;q=0.8
Connection: close
Upgrade-Insecure-Requests: 1
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
现在我被重定向到主机的登陆页面。这意味着主机发现请求不正常并将其重定向到登录页面。在浏览器中,我仍然可以看到我的正常页面:
HTTP/1.1 301 Moved Permanently
Location: http://www.lima-city.de/?utm_source=XX.XX.XX.XXX&utm_medium=webspace&utm_campaign=websites_redirect
Cache-Control: no-cache
Transfer-Encoding: chunked
Pragma: no-cache
X-Lima-Id: XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
Content-Type: text/html
Date: Fri, 27 Jan 2017 21:50:42 GMT
Server: openresty
Set-Cookie: _lcp=a; Path=/; expires=Mon Mar 20 2034 13:02:58
Connection: close
b2
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
0
解
哦,我完成了标题字段主机错误。 我认为标题字段是针对客户端的地址。
这是完全错误的
: - )
我不需要其他字段:
GET /samplepath HTTP/1.1
sub.sample.net:443
现在我也得到了正确的答复:
HTTP/1.1 200 OK
<Headfields>
<Body>
对于一次性回复,使用值关闭
添加标题连接