android - 应用程序到 Web API 产生 400 无效的主机名

标签 android asp.net http asp.net-web-api

我有一个奇怪的问题。从我的 android 应用程序,我正在调用一个 ASP.NET Web API。我可以毫无问题地从应用程序发出 POST 请求。但是,当我去发出 GET 请求时,请求返回“400 bad request invalid hostname”。这很奇怪,因为我可以发出 POST 请求,而不是 get。我使用的 URL 是 http://10.0.2.2/webapp/api/controllername(10.0.2.2 因为 android 模拟器使用 127.0.0.1;这是已发布的解决方法)。我确实启用了互联网权限。

我目前正在调试 Web API,我可以调试发布请求。我可以在 Internet Explorer 中为获取请求手动输入 URL,然后点击服务器。这就是我知道它正在工作的方式。我有一个 application_beginrequest 的处理程序,只是为了查看我是否正在访问服务器,它用于发布请求。但我从来没有看到服务器收到 get 请求。

为了发出 Android GET 请求,我使用了以下内容(添加了带有附加信息的评论)。

//Creation of get request
HttpGet request = new HttpGet(url); //correct URL; verified
for(NameValuePair h : headers)
{
    //6 headers added; verified OK
    request.addHeader(h.getName(), h.getValue());
}
//Calls method shown below
return executeRequest(request, url);

 private ResponseInformation executeRequest(HttpUriRequest request, String url) throws Exception
{
    HttpClient client = new DefaultHttpClient();
    HttpResponse httpResponse;

    try {
        //Get the response and check the code/message
        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();
        ResponseInformation info; // just a DTO for my needs

        if (entity != null) {
            InputStream instream = entity.getContent();
            //Response is an XML document listing the 400 error information
            response = convertStreamToString(instream);
            info = new ResponseInformation(responseCode, response, message);
            instream.close();
        }
        else
            info = new ResponseInformation(responseCode, message);

        return info;

    }
    .
    .
}

所以我不确定是谁在拒绝?为什么我会得到这个错误?它永远不会触发 Application_BeginRequest,因此它永远不会进入我的 Web API 服务,但 POSTS 确实……太奇怪了。

最佳答案

我在使用 Web API 自托管时遇到了同样的问题,无法发送 GET 或 POST 请求。

如果传入主机和 Host header 与已配置的 IIS 或自托管主机名,参见this . 我在主机中使用 RawCap(WireShark 无法在 Windows 中监控环回 IP),在 Android 模拟器中使用 tcpdumpHost header 以某种方式自动设置为 10.0.2.2。

使用 Fiddler 看不到此问题,因为您可以通过 127.0.0.1 获得低谷,而无需 10.0.2.2 解决方法。

只有当我们使用 10.0.2.2 特殊 IP 并且 Android 应用程序中的 Http 客户端库自动将 Host header 设置为 IP 时才会发生。就我而言,我使用 Retrofit由于 Host header 10.0.2.2 与 127.0.0.1 或本地主机不同,Web API 产生 400 无效主机名。

然后我将 Host header 覆盖为 localhost:port,它就可以正常工作了:

RestAdapter.Builder builder= new RestAdapter.Builder()
.setRequestInterceptor(new RequestInterceptor() {
     @Override
     public void intercept(RequestFacade request) {
         if (HOST.contains("10.0.2.2")) { // HOST = "http://10.0.2.2/api";
             request.addHeader("Host", "localhost");
         }            
     }
 });

只需确保您的 Android 应用中的 Host header 正确即可。

关于android - 应用程序到 Web API 产生 400 无效的主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14167998/

相关文章:

css - CSS @import 会暴露用户的 IP 地址吗?

网络丢失后Android暂停异步任务

asp.net - 了解 runat 服务器属性

c# - .NET 在异步任务完成之前返回响应

asp.net - 使用gridview时"must declare scalar variable"

linux - 如何使用 cURL 测试并等待 HTTP 服务?

java - 在android中向HTTPservlet发送数据

android - Android 上的对话框是异步的吗?

android - 如何以编程方式在约束布局中将 View 放在其他人的背面

android - Gradle 构建错误,错误 :Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'