Error 400 Bad request when accessing Web Api from Xamarin debug device

跟風遠走 提交于 2021-01-28 20:49:08

问题


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.

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);
}

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

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


回答1:


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

First enable external requests: IIS Express enable external request

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




回答2:


I had the same problem everything is working good on the browser but connot connect from emulator (i get a 400 bad request) it was very simple i just forgot to launch my web api as administrator



来源:https://stackoverflow.com/questions/15724582/error-400-bad-request-when-accessing-web-api-from-xamarin-debug-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!