2011-04-08 66 views
0

一個網站,我讀了檢索Java中的網頁,它是快速的使用方法:檢索添加了用戶輸入

URL url = new URL("http://example.com"); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.connect(); 

InputStream stream = connection.getInputStream(); 
// read the contents using an InputStreamReader 

但我怎麼添加用戶到URL給一個變量?例如:

用戶輸入x 加載頁面http://example.com/x.php

我是新來的Java,所以任何幫助,將不勝感激。

+0

我記得有可能在Java中連接兩個字符串爲多,不能使用這種技術? url + inputString成一個新的URL變量,這對你有意義嗎? – Bastardo 2011-04-08 13:33:14

回答

0
URL url = new URL("http://example.com"); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
URL urlWithInput = new URL("http://example.com" + input); 
connection.setRequestMethod("GET"); 
connection.connect();  
InputStream stream = connection.getInputStream();  

或者

String url = "http://example.com" + "/" + input 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
URL urlWithInput = new URL(url); 
connection.setRequestMethod("GET"); 
connection.connect();  
InputStream stream = connection.getInputStream();  
+0

嗯......你對我有更多的瞭解。你會推薦我使用哪一個? – rossisnotthemesia 2011-04-08 13:51:37

+0

第二個會更好,祝你好運。 – Bastardo 2011-04-08 13:53:14

+0

乾杯:DDDDDDD – rossisnotthemesia 2011-04-08 15:21:09

1

您可以使用字符串連接這樣的:

final String url = "http://example.com" + "/" + userInput 

然後你就可以實例化URL與字符串。

+0

爲快速響應而歡呼,我會盡快回復更多問題:D – rossisnotthemesia 2011-04-08 13:47:11