从 Xamarin 调试设备访问 Web Api 时出现错误 400 错误请求 [英] Error 400 Bad request when accessing Web Api from Xamarin debug device

查看:44
本文介绍了从 Xamarin 调试设备访问 Web Api 时出现错误 400 错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从在我的本地主机上运行的 WebApi 应用程序获得响应.我可以从 thr 浏览器获取它,也可以从另一个网络应用获取它.

I am trying to get a response from WebApi application running on my localhost. I can get it from thr browser and I can get it from another web app.

但是,我在 Xamarin Studio 调试设备中遇到相同代码的问题

I am, however, having problem with the same code in Xamarin Studio debugging device

var url="http://192.168.2.4:13325/api/values";
using (WebClient wc = new WebClient())
{

    wc.Headers.Add("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    wc.Headers.Add ("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22");
    wc.Headers.Add ("Accept-Encoding","gzip,deflate,sdch");
    wc.Headers.Add ("Accept-Language","en-US,en;q=0.8");
    wc.Headers.Add ("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.3");

    string HtmlResult = wc.DownloadString(url);
}

这是从浏览器发出请求时从 Fiddler 获取的标头.网络应用程序甚至不需要这些并获得响应.

This is the header taken from Fiddler when doing request from browser. The web app doesn't even need those and gets the response.

我正在使用计算机的 IP,因为我读到调试器运行虚拟机,因此我无法定位 localhost.

I am using my computer's IP, because I read, that the debugger runs a virtual machine, so I can't target localhost.

推荐答案

我解决了这个问题.与 Xamarin 或 android 无关,问题出在服务端.

I solved this probolem. It was not related to Xamarin or android, problem was on the service side.

首先启用外部请求:IIS Express 启用外部请求

然后为您的应用编辑绑定信息:IISExpress 返回 503来自远程机器的错误

An then edit binding information for your app: IISExpress returns a 503 error from remote machines

这篇关于从 Xamarin 调试设备访问 Web Api 时出现错误 400 错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆