这是请求应该看起来的位置(在顶部):
这是我在Java中的代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) throws Exception{
URL url = new URL("http://ishin-global.aktsk.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("/ping ", " HTTP/1.1");
conn.setRequestProperty("Host"," ishin-global.aktsk.com");
conn.setRequestProperty("Accept"," */*");
conn.setRequestProperty("X-Platform"," android");
conn.setRequestProperty("X-ClientVersion"," 3.1.2");
conn.setRequestProperty("X-Language"," en");
conn.connect();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
问题是我没有收到服务器的响应,但是我收到了这些错误:
Exception in thread "main" java.io.FileNotFoundException: http://ishin-global.aktsk.com
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1836)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at Main.main(Main.java:24)
我一直无法理解,所以我在这里问。谢谢提前
答案 0 :(得分:1)
conn.setRequestProperty("/ping ", " HTTP/1.1");
不正确。 /ping
不是HTTP请求标头,而是URL的路径。 URL的路径是URL本身的一部分,而不是标题:
URL url = new URL("http://ishin-global.aktsk.com/ping");
删除conn.setRequestProperty("/ping ", " HTTP/1.1");
行。 Java(以及每个HTTP客户端)在连接时会自动发送正确的初始行。