2011-04-08 68 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