[英]Why the same http request works in browser and fails when sent from my Android app?
I'm trying to fetch jSON data from Directus Api and requests are working fine when made from the browser. 我正在尝试从Directus Api提取jSON数据,并且从浏览器发出请求时,请求工作正常。 However, when I send the very same requests from my android app (identical URLs, with 'Authorization' header or with parameters) - the requests fail with java.io.FileNotFoundException and response code 405.
但是,当我从我的android应用发送完全相同的请求(相同的URL,带有“ Authorization”标头或带有参数)时,这些请求将失败,并显示java.io.FileNotFoundException和响应代码405。
The URL with parameters: WORKS IN BROWSER 带有参数的URL:浏览器中的WORKS IN BROWSER
http://example.com/dev/public/_/items/cities/1?access_token=my_test_token http://example.com/dev/public/_/items/cities/1?access_token=my_test_token
The same request with parameters: FAILS IN ADNROID 带有参数的相同请求:FAILS IN ADNROID
Uri.Builder builder = new Uri.Builder();
builder.appendQueryParameter("access_token","my_test_token");
//..
URL url = new URL(serverURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("Content-Type", "application/json; UTF-8");
conn.addRequestProperty("Accept","application/json; charset=utf-8");
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
The URL without parameters, sent with header 'Authorization: Bearer my_test_token': WORKS IN BROWSER 不带参数的URL,以标头'Authorization:Bearer my_test_token'发送:浏览器中的工作
http://example.com/dev/public/_/items/cities/1 http://example.com/dev/public/_/items/cities/1
The same request with 'Authorization: Bearer my_test_token' header: FAILS IN ANDROID 具有“ Authorization:Bearer my_test_token”标头的相同请求:ANDROID失败
URL url = new URL(serverURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("Authorization", "Bearer " + token);
conn.addRequestProperty("Content-Type", "application/json; UTF-8");
conn.addRequestProperty("Accept","application/json; charset=utf-8");
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
Non of the above requests (with parameters or with 'Authorization') work in Android. 以上所有请求(带有参数或带有“授权”的请求)都无法在Android中使用。 Is there any difference between browser and Android requests?
浏览器和Android请求之间有什么区别吗? Why the same request fails on Android giving java.io.FileNotFoundException but works in the browser?
为什么相同的请求在Android上失败,给出java.io.FileNotFoundException但在浏览器中有效? Can anyone help?
有人可以帮忙吗? Thanks.
谢谢。
我发现我正在使用POST并尝试在允许的方法为GET时发送输出流,因此服务器返回405'methodNotAllowed'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.