Response 解析,如果链接并不正常的返回解析方式

正常的网络链接的,客户端对于服务端的链接的接收为:

getInputStream();

而对于非正常链接的,如果服务端有向客户端发送内容,则其解析方式为:

getErrorStream();

但是,以上两个stream的返回都有可能为空。

From the java documentation on getErrorStream():

Returns the error stream if the connection failed but the server sent useful data nonetheless. The typical example is when an HTTP server responds with a 404, which will cause a FileNotFoundException to be thrown in connect, but the server sent an HTML help page with suggestions as to what to do. This method will not cause a connection to be initiated. If the connection was not connected, or if the server did not have an error while connecting or if the server had an error but no error data was sent, this method will return null. This is the default.

以下为正确的解析方式,注意,使用的是HttpURLConnection.HTTP_OK
InputStream is = null;
int responseCode = urlConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
    is = urlConnection.getInputStream();
}else{
    is = urlConnection.getErrorStream();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值