Reputation:
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
<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.
Upvotes: 1
Views: 1138
Reputation: 2246
Host ark.intel.com is using https so you should use port 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");
Upvotes: 1
Reputation: 9651
There is a missing \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.
Try using an HttpURLConnection
instead of plain sockets:
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();
}
Upvotes: 1