我使用以下代码转发GET请求:
public String getHTML(String urlToRead) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
但这一行:
getHTML("http://localhost:8080/myapp/serv/create");
导致此错误:
java.lang.IllegalStateException: Target host must not be null, or set in parameters.
我认为这是因为我使用的是localhost这个词?因此无法正确解析URL?我要求此代码在多台不同的机器上运行,因此不能使用我的IP,因为每台机器都有唯一的IP。是否还有另一种替代方案