2013-03-06 108 views
0

我目前正在編寫程序與我的網絡中的設備進行通信,並且以下代碼是我到目前爲止,它通過身份驗證,並可以從設備獲取網頁,但我無法獲得GET請求來工作,當我運行下面的代碼,我得到的錯誤:使用Get請求與URLConnection

Exception in thread "main" java.io.FileNotFoundException: http://192.168.100.222:80 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 

當網頁上,其等效要去http://l192.168.xxx.xxx/2?A=3&p=1&X=1234我輸入數據,並從tcpflow,它GET /2?A=4&p=1&X=1234 HTTP/1.1, 我嘗試創建一個新的與http://192.168.xxx.xxx/2?A=3&p=1&X=1234的網址連接,它的工作,但我有多個輸入選項,我不想爲他們每個人創建一個新的連接,我怎麼能做同等的保持聯繫?或者我在代碼中做了什麼錯誤?

在此先感謝。

public class main { 
public static void main(String[] argv) throws Exception { 
    Authenticator.setDefault(new MyAuthenticator()); 
    URL url = new URL("http://192.168.xxx.xxx"); 
    URLConnection connection = url.openConnection(); 
    connection.setDoOutput(true); 
    connection.setDoInput(true); 
    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); 
    out.write("Get /2?A=4&p=1&X=1234 HTTP1.1"); 
    out.close();   
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
    String decodedString; 
    while ((decodedString = in.readLine()) != null) { 
      System.out.println(decodedString); 
    } 
    in.close(); 
} 
+0

它沒有任何問題對我完美的工作。我嘗試與本地主機,而不是在網絡。此外,我沒有使用'Authenticator' – 2013-03-06 22:31:33

+0

有沒有可能的問題,你可以想到? – 3v01 2013-03-07 20:27:34

+0

刪除'Authenticator'並檢查 – 2013-03-07 20:30:44

回答

0

I don't want to create a new connection for each of them

不要擔心。 HttpURLConnection連接池在引擎蓋下。只需使用您的實際URL,不要試圖想出Java。

+0

有什麼東西重新鏈接的網址?就像'url = http://192.168.xxx.xxx/2?A = 3&p = 1&X = 1234'? – 3v01 2013-03-07 20:25:12

+0

@ 3v01請用標準英語重申您的問題。我不知道你在說什麼。 – EJP 2013-03-07 22:42:56

+0

有沒有一種方法來重新定義'url'中的url? – 3v01 2013-03-08 00:16:07