[英]Request Timeout - socket Java
Having some problems with the following code: 以下代码存在一些问题:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
class Main {
public static void main(String args[]) {
try {
Socket s = new Socket("ark.intel.com", 80);
PrintWriter out = new PrintWriter(s.getOutputStream());
out.write("GET / HTTP/1.1\r\n"
+ "Host: ark.intel.com\r\n"
+ "Connection: keep-alive\r\n"
+ "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36\r\n"
+ "Accept: text/html"
+ "\r\n");
out.flush();
BufferedReader read = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
String data = "";
while ((line = read.readLine()) != null) {
data += line;
System.out.println(line);
} read.close();
} catch (Exception error) {}
}
}
i keep getting 我不断
HTTP/1.0 408 Request Time-out Server: AkamaiGHost Mime-Version: 1.0 Date: Tue, 06 Feb 2018 11:43:41 GMT Content-Type: text/html Content-Length: 218 Expires: Tue, 06 Feb 2018 11:43:41 GMT HTTP / 1.0 408请求超时服务器:AkamaiGHost Mime版本:1.0日期:2018年2月6日星期二11:43:41 GMT内容类型:text / html内容长度:218过期:2018年2月6日星期二11:格林尼治标准时间43:41
<HTML><HEAD>
<TITLE>Request Timeout</TITLE>
</HEAD><BODY>
<H1>Request Timeout</H1>
The server timed out while waiting for the browser's request.<P>
Reference #2.403b3717.1517917421.0
</BODY></HTML>
any idea on how to fix this? 关于如何解决这个问题的任何想法?
Edit: The host ark.intel.com is using HTTPS, i need to find a way to send the requests over HTTPS rather then HTTP. 编辑:主机ark.intel.com使用HTTPS,我需要找到一种通过HTTPS而非HTTP发送请求的方法。
Host ark.intel.com is using https so you should use port 443. 主机ark.intel.com使用https,因此您应该使用端口443。
Socket s = new Socket("ark.intel.com",443);
Then you will get another error, that will not be visible, because you are not handling exception 然后,您将收到另一个错误,该错误将不可见,因为您没有处理异常
} catch (Exception error) {}
Here is example how to handle secure connections with use of SSL Socket: https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/samples/sockets/client/SSLSocketClient.java What you need to do is to change request with your parameters in this line out.println("GET / HTTP/1.0"); 以下是使用SSL套接字处理安全连接的示例: https : //docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/samples/sockets/client/SSLSocketClient.java要做的就是用这一行的参数更改请求out.println(“ GET / HTTP / 1.0”);
There is a missing \\r\\n: 缺少\\ r \\ n:
+ "Accept: text/html\r\n"
UPDATE 更新
As we have seen, this will allow you to do the request, but you will get a redirect, forcing you to use HTTPS. 如我们所见,这将允许您执行请求,但是您将获得重定向,从而迫使您使用HTTPS。
Try using an HttpURLConnection
instead of plain sockets: 尝试使用HttpURLConnection
而不是普通套接字:
try {
URL url = new URL("https://ark.intel.com/");
HttpURLConnection cnt = (HttpURLConnection) url.openConnection();
cnt.setRequestProperty("Host", "ark.intel.com");
cnt.setRequestProperty("Connection", "keep-alive");
cnt.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
cnt.setRequestProperty("Accept", "text/html");
int stat = cnt.getResponseCode();
if (stat != 200) {
throw new IOException("HTTP error " + stat);
}
BufferedReader read = new BufferedReader(new InputStreamReader(cnt.getInputStream()));
String line;
String data = "";
while ((line = read.readLine()) != null) {
data += line;
System.out.println(line);
} read.close();
} catch (Exception error) {
error.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.