String c="ddddddddddddd";
try {
// surl="http://www.baidu.com";
URL url = new URL("http://33gfgfdfdfdfdf33.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(4500);
conn.setReadTimeout(4500);
// conn.connect();
conn.getInputStream();
c = new Integer(conn.getResponseCode()).toString();
String message = conn.getResponseMessage();
System.out.println("getResponseCode code ="+ c);
System.out.println("getResponseMessage message ="+ message);
if(!c.startsWith("2")){
throw new Exception("ResponseCode is not begin with 2,code="+c);
}
}catch(Exception ex){
System.out.println(c);
ex.printStackTrace();
}
conn.getResponseCode())返回服务器响应状态码
conn.getinputstream() 会抛出此异常
ddddddddddddd
java.net.UnknownHostException: 33gfgfdfdfdfdf33.com
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
可帮助文档是这样的,并没有说会抛出像上面这样的异常 ,这不坑人吗?
InputStream java.net.URLConnection.getInputStream() throws IOException
getInputStream
public InputStream getInputStream()
throws IOException
Returns an input stream that reads from this open connection