工作学习 问题总结 java httpurlconnection.getinputstream

来源:互联网 发布:淘宝二手ipad哪家靠谱 编辑:程序博客网 时间:2024/06/01 09:49
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() 会抛出此异常

dddddddddddddjava.net.UnknownHostException: 33gfgfdfdfdfdf33.comat 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 IOExceptiongetInputStreampublic InputStream getInputStream()                           throws IOExceptionReturns an input stream that reads from this open connection