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

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值