zoe*_*vas 5 android filenotfoundexception http-post httpurlconnection http-status-code-400
我正在android中向 Url https://nitos.gr发出 Http POST 请求。
该url没有域名。Stackoverflow 不允许我在文本中包含带有数字的 URL,因此我只是编写一个随机 URL https://nitos.gr来作为示例。
我收到400 响应代码,并打印 getErrorStream() 给出 libcore.net.http.UnknownLengthHttpInputStream@41eba330
但是,我成功执行了 HTTP GET 请求。url 是https,因此我有一个允许所有 SSL 连接的假信任管理器。
总之:
协议: HTTPS
GET请求:成功
POST 请求:失败
请求代码: 400
错误消息: java.io.FileNotFoundException: https: //nitos.gr
错误流: java.io.FileNotFoundException: https: //nitos.gr
执行 POST 请求的方法如下:
public void setTestbedData(String path, String data) {
HttpURLConnection con = null;
try {
con = (HttpURLConnection) ( new URL(Constants.BASE_URL + path)).openConnection();
// If you invoke the method setDoOutput(true) on the URLConnection, it will always use the POST method.
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-Type", "application/json");
Log.i("data", data);
OutputStream outputStream = con.getOutputStream();
outputStream.write(data.getBytes());
outputStream.flush();
Log.w("RESPONSE CODE", "code " + con.getResponseCode());
Log.w("this is connection ",""+con);
InputStream errorstream = con.getErrorStream();
Log.w("GetErrorStream ", ""+errorstream);
InputStream _is;
if (con.getResponseCode() >= 400) {
_is = con.getInputStream();
} else {
_is = con.getErrorStream();
String result = getStringFromInputStream(_is);
Log.i("Error != 400", result);
}
if (con.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ con.getResponseCode());
}
BufferedReader responseBuffer = new BufferedReader(new InputStreamReader((con.getInputStream())));
String output;
Log.i("TestbedHttpClient","Output from Server:");
while ((output = responseBuffer.readLine()) != null) {
Log.i("Output",output);
}
con.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
响应代码(18936):代码 400 libcore.net.http.HttpsURLConnectionImpl$HttpUrlConnectionDelegate:https: //nitos.gr GetErrorStream(18936):libcore.net.http.UnknownLengthHttpInputStream@41eba330 System.err(18936):java.io。 FileNotFoundException:https: //nitos.gr System.err(18936):位于 libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
替换以下代码
Log.w("this is connection ",""+con);
InputStream errorstream = con.getErrorStream();
Log.w("GetErrorStream ", ""+errorstream);
InputStream _is;
if (con.getResponseCode() >= 400) {
_is = con.getInputStream();
} else {
_is = con.getErrorStream();
String result = getStringFromInputStream(_is);
Log.i("Error != 400", result);
}
Run Code Online (Sandbox Code Playgroud)
和
InputStream _is;
if (con.getResponseCode() / 100 == 2) { // 2xx code means success
_is = con.getInputStream();
} else {
_is = con.getErrorStream();
String result = getStringFromInputStream(_is);
Log.i("Error != 2xx", result);
}
Run Code Online (Sandbox Code Playgroud)
或者换句话说:getInputStream()当HTTP状态码大于或等于400时,从读取没有意义。事情没那么简单,也许你还必须处理一些3xx代码。查看HTTP 状态代码列表。
最后一句话:如果您觉得使用起来很麻烦,您可以使用此处HttpURLConnection列出的抽象库之一。
| 归档时间: |
|
| 查看次数: |
8001 次 |
| 最近记录: |