httpclient postasync windows phone 8.1 universal app second call 404 error

橙三吉。 提交于 2019-12-11 12:53:21

问题


I have a problem with httpclient in a universal windows phone 8.1 app. I've been looking but i have not a valid solution in any post.

the problem is that, when i call to the web service first time runs correctly, but when i call it, second or third time, gives me an error 404.

Until you restart the application will not run again.

i need to send that data in a post function cause i want to send a xml formated to string.

my code is very simple:

var handler = new HttpClientHandler
        {
            Credentials = new
                NetworkCredential("user", "pass", "domain")
        };


        using (var client = new HttpClient(handler))
        {
            var formContent = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair<string, string>("XMLText", XMLText),
                        new KeyValuePair<string, string>("param1", "textParam1")
                    });

            HttpResponseMessage response = new HttpResponseMessage();
            response = await client.PostAsync(URL, formContent);
            var responseString = response.Content.ReadAsStringAsync().Result;
            MessageDialog msgbox = new MessageDialog(responseString);
            await msgbox.ShowAsync();
        }

and my web service is even simpler:

[WebMethod]
    public String SetEnvioXML(string XMLText, string param1)
    {
        return XMLText;
    }

Any solution?

Sorry for my english and thaks for all!

Any help is welcome!


回答1:


Try to use the below code may be this will works, First try to run this on Google Rest Client or Post Man

            HttpRequestMessage httpRequest = new HttpRequestMessage();
            httpRequest.Method = HttpMethod.Post;
            httpRequest.RequestUri = URL;
            httpRequest.Content = formContent ;

            response  =await client.SendAsync(httpRequest);



回答2:


Finally i found the solution, i changed the autentication from windows autentication, to basic autentication in IIS, setting the domain in it. Then i try that:

client = new HttpClient();
var authHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", "usuario", "contraseña")))); client.DefaultRequestHeaders.Authorization = authHeader;

And it rules. Thanks for all of your answers!



来源:https://stackoverflow.com/questions/30934803/httpclient-postasync-windows-phone-8-1-universal-app-second-call-404-error

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