Java 如何获取HttpURLConnection错误响应代码
当我看这本书时,我的问题的典型答案是:Java 如何获取HttpURLConnection错误响应代码,java,android,http,Java,Android,Http,当我看这本书时,我的问题的典型答案是: HttpURLConnection httpConn = (HttpURLConnection)_urlConnection; InputStream _is; if (httpConn.getResponseCode() == 200) { _is = httpConn.getInputStream(); } else { /* error from server */ _is = httpConn.getErrorStream
HttpURLConnection httpConn = (HttpURLConnection)_urlConnection;
InputStream _is;
if (httpConn.getResponseCode() == 200) {
_is = httpConn.getInputStream();
} else {
/* error from server */
_is = httpConn.getErrorStream();
}
从
但是,当我查看HttpURLConnection.getResponseCode()
的实现时,它做的第一件事就是调用getInputStream()
。根据文档getInputStream()
我错过什么了吗?如果出现错误,如果给出响应代码的函数在这种情况下抛出,我应该如何获取响应代码
下面是java.net.HttpURLConnection.java中的getResponseCode()
对我来说的样子:
public int getResponseCode() throws IOException {
// Call getInputStream() first since getHeaderField() doesn't return
// exceptions
getInputStream();
String response = getHeaderField(0);
if (response == null) {
return -1;
}
response = response.trim();
int mark = response.indexOf(" ") + 1;
if (mark == 0) {
return -1;
}
int last = mark + 3;
if (last > response.length()) {
last = response.length();
}
responseCode = Integer.parseInt(response.substring(mark, last));
if (last + 1 <= response.length()) {
responseMessage = response.substring(last + 1);
}
return responseCode;
}
public int getResponseCode()引发IOException{
//由于getHeaderField()不返回,因此首先调用getInputStream()
//例外情况
getInputStream();
字符串响应=getHeaderField(0);
如果(响应==null){
返回-1;
}
response=response.trim();
int mark=response.indexOf(“”+1;
如果(标记==0){
返回-1;
}
int last=标记+3;
if(last>response.length()){
last=response.length();
}
responseCode=Integer.parseInt(response.substring(mark,last));
如果(last+1),您显示的是sun旧的实现
公共抽象类HttpURLConnection扩展了URLConnection
新的HttpURLConnection实现
用于Android和Java应用程序的HTTP&HTTP/2客户端。有关更多信息,请参阅网站和wiki
注:如果您想读取错误蒸汽流:
/**
* from sun.misc.IOUtils class.
* It's nearly twice as fast as the common implementation using ByteBuffers:
* */
public static byte[] readFully(InputStream is, int length, boolean readAll) throws IOException {
byte[] output = {};
if (length == -1) length = Integer.MAX_VALUE;
int pos = 0;
while (pos < length) {
int bytesToRead;
if (pos >= output.length) { // Only expand when there's no room
bytesToRead = Math.min(length - pos, output.length + 1024);
if (output.length < pos + bytesToRead) {
output = Arrays.copyOf(output, pos + bytesToRead);
}
} else {
bytesToRead = output.length - pos;
}
int cc = is.read(output, pos, bytesToRead);
if (cc < 0) {
if (readAll && length != Integer.MAX_VALUE) {
throw new EOFException("Detect premature EOF");
} else {
if (output.length != pos) {
output = Arrays.copyOf(output, pos);
}
break;
}
}
pos += cc;
}
return output;
}
/**
*来自sun.misc.IOUtils类。
*它的速度几乎是使用ByteBuffers的常见实现的两倍:
* */
公共静态字节[]已就绪(InputStream为,int-length,boolean readAll)引发IOException{
字节[]输出={};
如果(长度==-1)长度=Integer.MAX_值;
int pos=0;
while(pos=output.length){//仅在没有空间时展开
bytesToRead=Math.min(长度-pos,output.length+1024);
if(output.length
您所展示的是sun的旧实现
公共抽象类HttpURLConnection扩展了URLConnection
新的HttpURLConnection实现
用于Android和Java应用程序的HTTP&HTTP/2客户端。有关更多信息,请参阅网站和wiki
注:如果您想读取错误蒸汽流:
/**
* from sun.misc.IOUtils class.
* It's nearly twice as fast as the common implementation using ByteBuffers:
* */
public static byte[] readFully(InputStream is, int length, boolean readAll) throws IOException {
byte[] output = {};
if (length == -1) length = Integer.MAX_VALUE;
int pos = 0;
while (pos < length) {
int bytesToRead;
if (pos >= output.length) { // Only expand when there's no room
bytesToRead = Math.min(length - pos, output.length + 1024);
if (output.length < pos + bytesToRead) {
output = Arrays.copyOf(output, pos + bytesToRead);
}
} else {
bytesToRead = output.length - pos;
}
int cc = is.read(output, pos, bytesToRead);
if (cc < 0) {
if (readAll && length != Integer.MAX_VALUE) {
throw new EOFException("Detect premature EOF");
} else {
if (output.length != pos) {
output = Arrays.copyOf(output, pos);
}
break;
}
}
pos += cc;
}
return output;
}
/**
*来自sun.misc.IOUtils类。
*它的速度几乎是使用ByteBuffers的常见实现的两倍:
* */
公共静态字节[]已就绪(InputStream为,int-length,boolean readAll)引发IOException{
字节[]输出={};
如果(长度==-1)长度=Integer.MAX_值;
int pos=0;
while(pos=output.length){//仅在没有空间时展开
bytesToRead=Math.min(长度-pos,output.length+1024);
if(output.length
好奇。当我查看它时,它做的第一件事是查看responseCode
是否已设置,如果已设置,则返回它。下一件事是调用getInputStream()
,在一个块中尝试。我建议您再看一看。android实现是否可能与普通java实现不同?@Pragnani是的,它确实发生了变化。目前他们正在使用自己的实现。完整包名为java.net.HttpURLConnection(android-21)@DaedalusAlpha-我给了您解释好奇。当我查看它时,它做的第一件事是查看responseCode
是否已经设置,如果已经设置,则返回它。下一件事是调用getInputStream()
,在一个块中尝试。我建议您再看一看。android实现是否可能与普通的java实现不同?@Pragnani是的,它确实发生了变化。目前他们正在使用自己的实现。完整包名为java.net.HttpURLConnection(android-21)@DaedalusAlpha-我给了您解释