2013-03-06 208 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