为什么会抛出 UnknownHostException?
parameters = "user=akirus&pass=1234&version=1";
String result = excutePost("http://russianimperial.ru/minecraft/loginServer.php", parameters);
public static String excutePost(String targetURL, String urlParameters) {
HttpURLConnection connection = null;
try {
URL url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuffer response = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
String str1 = response.toString();
return str1;
}catch (Exception e) {
e.printStackTrace();
return null;
}finally {
if (connection != null)
connection.disconnect();
}
}
网址http://russianimperial.ru/minecraft/loginServer.php有效。早些时候它工作得很好。UnknownHostException
扔在这个地方InputStream is = connection.getInputStream();
。在 javadoc 中编写的connection.getInputStream()
协议不支持输入时可以抛出此异常,但我认为 http 支持输入。