简体   繁体   English

400错误的请求无效的主机名仅在Android应用程序中

[英]400 bad request invalid hostname only in android application

I have a asp.net mvc website deployed on a server, providing a few web interfaces to others. 我在服务器上部署了一个asp.net mvc网站,为其他人提供了一些Web界面。 For example, getting the current user's information, my test C# console application looks like this: 例如,获取当前用户的信息,我的测试C#控制台应用程序如下所示:

using (var client = new WebClient())
{
     try
     {
         var url = "http://api.fake.mysite.com/v1.0/user/current";
         var token = "e0034e1c082de62b74e361b15f9c6471";
         var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(token));
         client.Headers["Authorization"] = encoded;
         client.Headers["Content-Type"] = "application/json";
         Console.WriteLine(client.DownloadString(url));
     }
     catch (WebException e)
     {
         //log the exception
     }
}

You can see the usage is pretty simple, just request the url via HTTP_GET, set the Authorization header to the encoded token. 您可以看到用法非常简单,只需通过HTTP_GET请求url,将Authorization标头设置为编码令牌即可。 Actually it works fine in my machine. 实际上,它在我的机器上工作正常。 But some one else meets a strange issue when visiting this url in an android application, here is the java code: 但是其他人在android应用程序中访问此url时遇到一个奇怪的问题,这是java代码:

HttpClient httpClient = new DefaultHttpClient();
String token = "e0034e1c082de62b74e361b15f9c6471";
String url = "http://api.fake.mysite.com/v1.0/user/current";
HttpGet httpGet = new HttpGet(url);
String encoded = Base64.encodeToString(token.getBytes(), Base64.DEFAULT);
httpGet.addHeader("Authorization", encoded);
httpGet.addHeader("Content-Type", "application/json");

try {    
    HttpResponse httpResponse = httpClient.execute(httpGet);
    int responseCode = httpResponse.getStatusLine().getStatusCode();
    String response = EntityUtils.toString(httpResponse.getEntity());
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

then he got "400 bad request invalid host name" error. 然后他收到“ 400错误的请求无效的主机名”错误。 I've tried: 我试过了:

(1) make sure the variable "encoded" has the same value in C# and Java code. (1)确保变量“ encoded”在C#和Java代码中具有相同的值。

(2) make sure the website's domain name is correctly set in server IIS (2)确保在服务器IIS中正确设置了网站的域名

(3) all PCs/mobile phones can visit the test index page( http://api.fake.mysite.com ) (3)所有PC /手机均可访问测试索引页面( http://api.fake.mysite.com

(4) ping api.fake.mysite.com works fine (4)ping api.fake.mysite.com工作正常

(5) if removing httpGet.addHeader("Authorization", encoded); (5)如果删除httpGet.addHeader("Authorization", encoded); , the Java program got a 401 Unauthorized result as expected(the server code under my control returns the result) ,则Java程序得到了预期的401未经授权的结果(我控制下的服务器代码返回了结果)

(6) some other applications using C# and PHP can use the web methods well, only android application can't(tested in two totally different android mobile phones, the android emulator got 400 invalid host name either) (6)其他使用C#和PHP的应用程序可以很好地使用Web方法,只有android应用程序不能(在两个完全不同的android手机中测试,android模拟器也获得了400个无效的主机名)

(7) use IP instead of domain name http://xx.xx.xx.xx/v1.0/user/current , everything is the same. (7)使用IP代替域名http://xx.xx.xx.xx/v1.0/user/current ,一切相同。 (xx.xx.xx.xx stands for the ip address) (xx.xx.xx.xx代表IP地址)

(8) checked the IIS log, all requests to /v1.0/user/current returns 200/401/500, no 400 results. (8)检查了IIS日志,所有对/v1.0/user/current的请求都返回200/401/500,没有400结果。

(9) make sure the android application has internet permissions(actually we've added all permissions) (9)确保android应用具有互联网权限(实际上我们已经添加了所有权限)

Does anyone know the reason or help to find the reason? 有谁知道原因或帮助找到原因? Thank you very much, this issue is driving me crazy. 非常感谢您,这个问题使我发疯。

Should be httpGet.addHeader("Authorization", "basic " + encoded); 应该是httpGet.addHeader("Authorization", "basic " + encoded); and String encoded = Base64.encodeToString(token.getBytes(), Base64.NO_WRAP); String encoded = Base64.encodeToString(token.getBytes(), Base64.NO_WRAP);

I struggled the very same problem. 我在同一个问题上挣扎。 I can send HTTP POST from Fiddler or any other tool to my asp.net web API in debug mode but I can not access from my android application. 我可以在调试模式下将HTTP POST从Fiddler或任何其他工具发送到我的asp.net Web API,但无法从android应用程序访问。

  1. I tried to be sure to connect from my computer browser to web API interface. 我试图确保从计算机浏览器连接到Web API界面。

  2. I tried to be sure to connect from android emulator web 我试图确保从android模拟器网络连接
    browser(AEWB). 浏览器(AEWB)。 And then I deployed my web api to IIS so I can get certain address to access from AEWB. 然后,我将Web API部署到IIS,以便可以获取某些地址以从AEWB访问。

I can accessed to this adres from my AEWB 我可以通过我的AEWB访问此广告

http://10.0.0.2:8088/api/tran

http://10.0.0.2 -> this is your local host address seen from Android
8088  -> this is your port of web api hosted on IIS
/api  -> this is web api
/tran -> this is your controller

我可以使用AEWB从IIS地址获取json值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 400 Bad Request - 在本地进行nancy自托管Web API调用时无效的主机名 - 400 Bad Request - Invalid Hostname when making nancy self hosted Web API calls locally 集成测试在本地通过,但在 Azure Devops 上失败,请求为 400 Bad Request - Invalid Hostname C# - Integration tests pass locally but fail on Azure Devops with 400 Bad Request - Invalid Hostname C# Grapevine远程连接错误请求-无效的主机名 - Grapevine Remote Connection Bad Request - Invalid Hostname 表单无效时出现 400 错误请求 - 400 bad request when form is invalid 仅在Chrome上了解400错误请求 - Understanding a 400 bad request only on chrome localhost上的子域提供错误请求 - 无效的主机名错误 - Subdomain on localhost gives Bad Request - Invalid Hostname error (400) 错误请求:获取 Salesforce 的 AccessToken 错误:“invalid_grant” - (400) Bad Request : Get AccessToken of Salesforce error:"invalid_grant" 使用WCF和Mono的400错误请求(无效主机) - 400 Bad Request (Invalid Host) using WCF and mono IIS 7错误请求(错误400),当querystring具有无效日期时? - IIS 7 Bad Request (ERROR 400) when querystring has invalid date? 使用C#中的简单Web服务器400错误请求(无效主机) - 400 Bad Request (Invalid Host) using simple webserver in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM