httpclient postasync windows phone 8.1通用app二次调用404错误

httpclient postasync windows phone 8.1 universal app second call 404 error

我在通用 windows phone 8.1 应用程序中遇到 httpclient 问题。 我一直在寻找,但在任何 post.

中都没有有效的解决方案

问题是,当我第一次调用 Web 服务时 运行s 正确,但是当我第二次或第三次调用它时,出现错误 404。

除非您重新启动应用程序,否则不会再次 运行。

我需要在 post 函数中发送该数据,因为我想发送格式化为字符串的 xml。

我的代码很简单:

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

我的网络服务更简单:

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

有什么解决办法吗?

对不起我的英语,谢谢大家!

欢迎任何帮助!

尝试使用下面的代码可能会起作用, 首先在 Google Rest Client 或 Post Man

上尝试 运行
            HttpRequestMessage httpRequest = new HttpRequestMessage();
            httpRequest.Method = HttpMethod.Post;
            httpRequest.RequestUri = URL;
            httpRequest.Content = formContent ;

            response  =await client.SendAsync(httpRequest);

最后我找到了解决方案,我将身份验证从 windows 身份验证更改为 IIS 中的基本身份验证,并在其中设置域。然后我试试看:

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

它统治着。感谢您的所有回答!