连接到本地主机时Xamarin请求错误

zte4gxcn  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(96)

我有一些问题,连接到我的localhost API在我的xamarin应用程序的Android模拟器。
这是我的密码

public class ReservasConnection {

    private HttpClient client;

 
    private HttpClient GetLocalInstance() {

        HttpClientHandler clientHandler = new HttpClientHandler();
        clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };

        HttpClient _client = new HttpClient(clientHandler);

        _client.BaseAddress = new Uri("https://10.0.2.2:44365/reservasandroid/");
        _client.DefaultRequestHeaders.Accept.Clear();
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        return _client;
    }

    public VehiculosPreciosResponse GetVehiculosPrecio(VehiculosRequest datosRequest) {

        client = GetLocalInstance();

        string datosToJson = JsonConvert.SerializeObject(datosRequest);

        var content = new StringContent(datosToJson, Encoding.UTF8, "application/json");

        HttpResponseMessage response = client.PostAsync("GetVehiculosPrecios", content).Result;

        response.EnsureSuccessStatusCode();

        var vehiculosResponse = response.Content.ReadAsStringAsync().Result;

        return JsonConvert.DeserializeObject<VehiculosPreciosResponse>(vehiculosResponse);

    }

}

当使用这段代码时,我得到一个400 BadRequest作为响应。我认为问题出在API上,因为我们有另一个API,如果我尝试连接到另一个API,它可以正常工作,没有任何问题。这是API代码:

public class ReservasAndroidController : Controller
{

    string ConnectionString;
    int Minutos;

    ReservasHandler handler;

    public ReservasAndroidController() {

        ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
        Minutos = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Minutos"]);

        handler = new ReservasHandler();

    }

    [HttpPost]
    public JsonResult GetVehiculosPrecios(VehiculosAndroidRequest request) {
        UserConexiones usrConexiones = null;
        try {

            usrConexiones = UserConexiones.GetInstance();
            usrConexiones.AbrirConexion(ConnectionString);

            if(!LoginHandler.Validar_IdSession(request.SessionID, usrConexiones))
                return Json(new VehiculosPreciosAndResponse(2, "El token no es valido", new VehiculoPrecioAnd[0]), JsonRequestBehavior.AllowGet);

            var usuario = LoginHandler.getUserCostumer(request.SessionID, Minutos, usrConexiones);

            var vehiculos = handler.GetVehiculosPreciosAndroid(request.Datos, usuario.IdEmpresa, usrConexiones);

            return Json(new VehiculosPreciosAndResponse(1, "", vehiculos.ToArray()), JsonRequestBehavior.AllowGet);

        }catch(Exception ex) {
            ClGeneral.EscribeLog(ex);
            return Json(new VehiculosPreciosAndResponse(3, ex.Message, new VehiculoPrecioAnd[0]), JsonRequestBehavior.AllowGet);
        } finally {
            if (usrConexiones != null)
                usrConexiones.CerrarConexion();
        }
    }

}

但它甚至没有到达API并直接获得400状态。我认为我的API中缺少了一些配置,但我不知道可能是什么。这不是SSL问题,我通过使用HttpClientHandler并在其中设置CustomValidationCallback解决了这个问题。不工作的API是.NET Framework 4.8,工作的是.NET Framework 4.7.2。如果我尝试通过模拟器中的chrome访问API到测试端点,我会得到以下结果:

但如果我尝试在其他API中到达测试端点,它可以正常工作,没有任何问题:

vjrehmav

vjrehmav1#

通过检查设置,我发现了一个API和另一个API之间的区别。
.vs/[projectName]/config的解决方案文件夹中,文件applicationgost.config位于以下代码中:

<sites>
   ...
   <site name="[projectName]" id="2">
      ...
      <bindings>
        ...
        <binding protocol="https" bindingInformation="*:44365:localhost"/>
      </bindings>
   </site>
</sites>

为了使用模拟器,我们必须将localhost更改为127.0.0.1

相关问题