FileNotFoundException for HttpURLConnection in Ice Cream Sandwich

萝らか妹 提交于 2019-11-27 07:33:04

Try removing the setDoOutput call. Taken from this blog: a blog

Edit: This is needed when using a POST call.

A FileNotFoundException may also been thrown if the server returns a bad error code (e.g., 400 or 401). You can handle this as follows:

int responseCode = con.getResponseCode(); //can call this instead of con.connect()
if (responseCode >= 400 && responseCode <= 499) {
    throw new Exception("Bad authentication status: " + responseCode); //provide a more meaningful exception message
}
else {
    InputStream in = con.getInputStream();
    //etc...
}

I Don't know why, but dealing manually with redirection resolves the problem.

connection.setInstanceFollowRedirects(false);

A little late but you can also verify the accepted content. You can add this line to accept all kinds of contents

urlConnection.setRequestProperty("Accept","*/*");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!