You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Forms调用ASP.NET Web API出现HttpRequestException请求发送错误

Hey there, let's tackle this System.Net.Http.HttpRequestException issue you're hitting with your Xamarin Forms + ASP.NET Web API app! First, let's recap your setup to make sure we're on the same page:

  • You're building a mobile app with Xamarin Forms paired with an ASP.NET Web API
  • Both projects live in the same Visual Studio 2017 solution
  • The Web API was created using the template with individual user accounts
  • Your registration binding model looks like this:
public class RegisterBindingModel { 
    public string Email { get; set; } 
    public string Password { get; set; }
    // ... other properties
}

Below are the most common fixes and troubleshooting steps for this error:

Troubleshooting HttpRequestException in Your Setup

1. Fix the Web API Access URL (Don't Use localhost)

When running the Web API and Xamarin app side-by-side, localhost won't work for simulators or physical devices—they can't reach your computer's local loopback address directly:

  • For Android emulators: Replace localhost with 10.0.2.2 (this maps to your host machine's loopback)
  • For iOS simulators: Use your computer's local network IP (e.g., 192.168.1.100) instead of localhost
  • For physical devices: Ensure your phone and computer are on the same WiFi network, then use your computer's local IP address
  • Double-check your Web API's port number (found in the project's Properties > Debug > Launch URL) and include it in your request URL (e.g., http://192.168.1.100:5000/api/Account/Register)

2. Resolve HTTPS Certificate Trust Issues

If your Web API uses HTTPS (default for the individual accounts template), Xamarin apps won't trust the self-signed development certificate automatically:

  • Quick test: Temporarily disable HTTPS in your Web API (go to Properties > Debug and uncheck "Enable SSL") and use HTTP for your requests
  • Permanent fix for HTTPS: Export the Web API's certificate from your browser, then install and trust it on your simulator/physical device

3. Verify Route and Request Method Match

The individual accounts template's registration endpoint is typically api/Account/Register (for older ASP.NET Identity setups). Make sure:

  • Your Xamarin app is sending a POST request to this exact route
  • Your Web API's routing config in WebApiConfig.cs is correctly set up to handle controller-based routes:
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

4. Ensure Request Body Matches the Binding Model

Double-check that your Xamarin request sends a valid JSON body that matches all required properties in RegisterBindingModel:

  • Example valid request code in Xamarin:
var registerData = new { 
    Email = "user@example.com", 
    Password = "P@ssw0rd123",
    ConfirmPassword = "P@ssw0rd123" // Don't forget this if your model requires it!
};
var jsonContent = new StringContent(
    JsonConvert.SerializeObject(registerData),
    Encoding.UTF8,
    "application/json"
);

var response = await _httpClient.PostAsync("http://192.168.1.100:5000/api/Account/Register", jsonContent);

5. Get the Real Error from Inner Exception

HttpRequestException is often a wrapper for a more specific issue. Capture the inner exception to see the root cause:

try
{
    // Your request code here
}
catch (HttpRequestException ex)
{
    if (ex.InnerException != null)
    {
        // Log or print this message—it will tell you if it's a timeout, DNS error, or certificate issue
        Console.WriteLine($"Root Cause: {ex.InnerException.Message}");
    }
}

内容的提问来源于stack exchange,提问作者Chandru Vijay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:05:21