简体   繁体   English

为什么相同的http请求在浏览器中有效,但从Android应用发送时却失败?

[英]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.

相关问题 在 php 代码中发出请求时,URL 中带有用户名和密码的 http 请求失败,但可以在任何浏览器中使用 - http request with user and password in URL fails when request is made in php code, but works in any browser 从QueryPath访问OpenAmplify时,我的HTTP请求失败/超时。 为什么? - When Accessing OpenAmplify from QueryPath my HTTP request fails/times out. Why? 检查http请求是否来自我的Android应用程序 - Check if http request comes from my android app 似乎无法从我的Android应用发送HTTP获取参数以显示在我的PHP页面上 - Can't seem to get http get parameters sent from my Android app to display on my PHP page HTTP_Request调用在浏览器中有效,在Web应用程序中超时 - HTTP_Request call works in browser, timeout in web app 当 Http 请求从另一个 Laravel 应用程序发送时,Laravel 使用不正确的数据库 - Laravel uses incorrect database when Http request is sent from another Laravel app 当相同的请求在 Postman 中工作时,为什么我对此 API 的 PHP cURL 请求没有提取任何数据? - Why is my PHP cURL request to this API not pulling any data when the same request works in Postman? Android 应用程序进行 JSON 调用从服务器获取意外数据,但相同的调用在浏览器中有效 - Android app making JSON call gets unexpected data from server, but same call works in browser 为什么 HTTP 请求在 Tinker 中可以正常工作,但在 Laravel 7 的应用程序中却不行? - Why is the HTTP request works fine in Tinker but not in the app in Laravel 7? jQuery $ .getJSON http请求失败,同时使完全相同的请求手动工作 - jQuery $.getJSON http request fails, while making the exact same request manually works
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM