请求超时 - 套接字Java

时间:2018-02-06 11:45:58

标签: java sockets

以下代码遇到一些问题:

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) {}
    }
}

我一直在

HTTP / 1.0 408请求超时 服务器:AkamaiGHost 哑剧版:1.0 日期:2018年2月6日星期二,格林威治标准时间11:43:41 内容类型: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&#32;&#35;2&#46;403b3717&#46;1517917421&#46;0
</BODY></HTML>

关于如何解决这个问题的任何想法?

编辑: 主机ark.intel.com正在使用HTTPS,我需要找到一种通过HTTPS而不是HTTP发送请求的方法。

2 个答案:

答案 0 :(得分:1)

主机ark.intel.com正在使用https,因此您应该使用端口443。

Socket s = new Socket("ark.intel.com",443);

然后你会得到另一个错误,这个错误是不可见的,因为你没有处理异常

} catch (Exception error) {}

以下是如何使用SSL套接字处理安全连接的示例:https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/samples/sockets/client/SSLSocketClient.java您需要做的是使用此行中的参数更改请求out.println(“GET / HTTP / 1.0”);

答案 1 :(得分:1)

缺少\ r \ n:

        + "Accept: text/html\r\n"

更新

正如我们所见,这将允许您执行请求,但您将获得重定向,强制您使用HTTPS。

尝试使用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();
    }
相关问题