gpt4 book ai didi

c# - UWP 应用程序中的 PostAsync 不工作(未发送内容)

转载 作者:行者123 更新时间:2023-11-30 23:18:51 25 4
gpt4 key购买 nike

我有点卡住了,我正在尝试创建一个将 XML 内容发布到 Web 服务的 UWP 应用程序。我可以让它在常规的 .net 控制台应用程序中正常工作而不会出现问题。尝试使用 UWP 重新创建它被证明是棘手的。使用 fiddler ,我缩小了网络服务端点没有收到我的内容的范围。看起来 header 设置正确,内容长度已正确发送,但实际内容未发送。这是代码的核心,它在之后崩溃/抛出异常:

HttpResponseMessage ResponseMessage = await request.PostAsync(requestUri, httpContent2).ContinueWith(
(postTask) => postTask.Result.EnsureSuccessStatusCode());

当我尝试执行 PostASync 时,查看 fiddler,我得到:

HTTP/1.1 408 Request body incomplete
Date: Mon, 14 Nov 2016 15:38:53 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
Cache-Control: no-cache, must-revalidate
Timestamp: 10:38:53.430

请求正文不包含指定的字节数。得到0,预期617

我确信我得到的内容是正确的(我从一个文件中读取它,我将它发送到调试窗口以验证它是正确的)。我认为这可能与 HttpContent httpContent2 有关——在常规 .NET 中,我从来不需要使用它,但对于 PostAsync,我需要使用它。

如有任何想法,我们将不胜感激!

    public async void PostWebService()
{


string filePath = "Data\\postbody.txt";
string url = "https://outlook.office365.com/EWS/Exchange.asmx";

Uri requestUri = new Uri(url); //replace your Url

var myClientHandler = new HttpClientHandler();
myClientHandler.Credentials = new NetworkCredential("user@acme.com", "password");

HttpClient request = new HttpClient(myClientHandler);

string contents = await ReadFileContentsAsync(filePath);
Debug.WriteLine(contents);

HttpContent httpContent2 = new StringContent(contents, Encoding.UTF8, "text/xml");

string s = await httpContent2.ReadAsStringAsync();
Debug.WriteLine(s); //just checking to see if httpContent has the correct data

//HttpResponseMessage ResponseMessage = await request.PostAsync(requestUri, httpContent);
request.MaxResponseContentBufferSize = 65000;



HttpResponseMessage ResponseMessage = await request.PostAsync(requestUri, httpContent2).ContinueWith(
(postTask) => postTask.Result.EnsureSuccessStatusCode());

Debug.WriteLine(ResponseMessage.ToString());

}

最佳答案

看来我找到了问题的根本原因。这似乎是使用网络身份验证时 System.Net.Http.HttpClient 的一个已知错误。看这篇文章here

我最初的错误是我没有捕捉到 PostAsync 抛出的异常。一旦我将它包装在 try/catch block 中,我就会抛出以下异常:

“这个 IRandomAccessStream 不支持 GetInputStreamAt 方法,因为它需要克隆,而这个流不支持克隆。”

我在上面链接的文章的第一段指出:

When you use the System.Net.Http.HttpClient class from a .NETframework based Universal Windows Platform (UWP) app and send aHTTP(s) PUT or POST request to a URI which requires Integrated WindowsAuthentication – such as Negotiate/NTLM, an exception will be thrown.

The thrown exception will have an InnerException property set to themessage:

“This IRandomAccessStream does not support the GetInputStreamAt methodbecause it requires cloning and this stream does not support cloning.”

The problem happens because the request as well as the entity body ofthe POST/PUT request needs to be resubmitted during the authenticationchallenge. The above problem does not happen for HTTP verbs such asGET which do not require an entity body.

This is a known issue in the RTM release of the Windows 10 SDK and weare tracking a fix for this issue for a subsequent release.

对我有用的建议和解决方法是使用 Windows.Web.Http.HttpClient 而不是 System.Net.Http.HttpClient

使用该建议,以下代码对我有用:

      string filePath = "Data\\postbody.txt";
string url = "https://outlook.office365.com/EWS/Exchange.asmx";

Uri requestUri = new Uri(url); //replace your Url

string contents = await ReadFileContentsAsync(filePath);
string search_str = txtSearch.Text;

Debug.WriteLine("Search query:" + search_str);
contents = contents.Replace("%SEARCH%", search_str);

Windows.Web.Http.Filters.HttpBaseProtocolFilter hbpf = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
Windows.Security.Credentials.PasswordCredential pcred = new Windows.Security.Credentials.PasswordCredential(url, "username@acme.com", "password");
hbpf.ServerCredential = pcred;

HttpClient request = new HttpClient(hbpf);

Windows.Web.Http.HttpRequestMessage hreqm = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Post, new Uri(url));
Windows.Web.Http.HttpStringContent hstr = new Windows.Web.Http.HttpStringContent(contents, Windows.Storage.Streams.UnicodeEncoding.Utf8, "text/xml");
hreqm.Content = hstr;

// consume the HttpResponseMessage and the remainder of your code logic from here.


try
{
Windows.Web.Http.HttpResponseMessage hrespm = await request.SendRequestAsync(hreqm);

Debug.WriteLine(hrespm.Content);
String respcontent = await hrespm.Content.ReadAsStringAsync();

}
catch (Exception ex)
{
string e = ex.Message;
Debug.WriteLine(e);
}

希望这对遇到此问题的其他人有所帮助。

关于c# - UWP 应用程序中的 PostAsync 不工作(未发送内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40592683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com