Java 使用添加的用户输入检索网站

Java 使用添加的用户输入检索网站,java,html,html-parsing,Java,Html,Html Parsing,我了解到,要用java检索网页,可以快速使用: URL url = new URL("http://example.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); InputStream stream = connection.getInputStream(); // r

我了解到,要用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 加载页面


我是java新手,希望您能提供帮助。

您可以像这样使用字符串连接:

final String url = "http://example.com" + "/" + userInput
然后可以用该字符串实例化URL

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();      


我记得在Java中可以连接两个字符串,你不能使用这种技术吗?url+输入字符串到一个新的url变量中,这对您有意义吗?为快速响应干杯,我很快会回来回答更多问题:DAh。。。你对我来说更有意义了。你推荐我用哪一种?
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();