我读到用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新手,所以任何帮助将不胜感激。
最佳答案
您可以像这样使用字符串连接:
final String url = "http://example.com" + "/" + userInput
然后您可以使用该字符串实例化 URL。
关于java - 检索添加了用户输入的网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5595731/