2017-07-06 150 views
0

這是請求應該如何看(在頂部):問題與連接到URL

這是我在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) 

我一直沒能弄明白,所以我問here.Thanks提前

+2

在您的圖片中,網址以「https」開頭,而不是「http」。 – RealSkeptic

+0

你檢查了回覆代碼/消息嗎?即''conn.getResponseMessage()' – Serge

+0

我改變了'http'到'https',它仍然是相同的,我使用conn.getResponseMessage()時得到代碼404,但url有效 –

回答

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客戶端)在連接時會自動發送正確的首行。

+0

謝謝,它在我添加/平 –