POST request gives FileNotFoundException
POST request provides FileNotFoundException for URL
I am making Http POST request for Url https://nitos.gr in android .
The URL has no domain name. Stackoverflow doesn't allow me to include a URL with numbers in my text, so I'll just write a random url https://nitos.gr for example.
I am getting response code 400 and printing getErrorStream () providing libcore.net.http.UnknownLengthHttpInputStream@41eba330
However, I am successfully making an HTTP GET request. The url is https , so I have a fake trust manager allowing all SSL connections.
In short:
Protocol: HTTPS
GET request: successful
POST request: Failed
Request code: 400
Error message: java.io.FileNotFoundException: https://nitos.gr
ErrorStream: java.io.FileNotFoundException: https://nitos.gr
The method making the POST request follows:
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();
}
}
Error messages:
Response code (18936): code 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): at libcore.net.http.HttpURLConnectionImpl.getInputStream (HttpURLConnectionImpl.java:186)
source to share
Replace the following code
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);
}
from
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);
}
Or, to put it another way: there is no point in reading with getInputStream()
when the HTTP status code is 400 or greater. It's not that easy, you may have to deal with some 3xx codes. Have a look at the List of HTTP Status Codes .
HttpURLConnection
One final word: if you find it cumbersome to use, you can use one of the abstraction libraries listed here .
source to share