问题描述
我想进行服务器POST请求。
I would like to do serveral POST-Request.
我的问题是应用程序在 Task< ...>中随机崩溃; do_POST()
-在 async void bt_publishPath_Click()
-Event中 await
期间的功能。
My problem is that the Application crashes randomly in the Task<...> do_POST()
-Function during await
in my async void bt_publishPath_Click()
-Event.
我可以执行一些POST请求(一切正常),有时 await
会阻塞,应用崩溃与 System.NullReferenceException
(经过长时间的等待)。我不明白为什么会这样,因为它是第一次工作。
I can do some POST-Requests (everything's working fine) and sometime the await
will block and the app crashes with a System.NullReferenceException
(after a long time of waiting). I don't understand why this is happing because the first times it is working.
这是我到目前为止所得到的:
我已注释掉所有不重要的功能。我已经测试了没有这些代码行的代码,并且也发生了错误。
// MainWindow
private async void bt_publishPath_Click(object sender, RoutedEventArgs e)
{
// gr_spinner.Visibility = Visibility.Visible;
ApiResponse res = await ApiCall.do_POST(
"/path",
getBasicAuthString(),
flink.getJsonString()
);
// gr_spinner.Visibility = Visibility.Collapsed;
// Globals.errorHandling(res.Statuscode, res.Parameters[0]);
}
// ApiCall static Class
public static Task<ApiResponse> do_POST(string apiPath, string auth, string json)
{
return Task.Run(() =>
{
int statuscode = 0;
HttpWebResponse response = null;
try
{
// HEADER
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiBase + apiPath);
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("Authorization", auth);
// DATA
byte[] data = Encoding.ASCII.GetBytes(json);
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
// SEND AND GET RESPONSE
/*
* It seems to be that GetResponse() isn't called correctly.
* The Backend doesn't receive a POST-request and the reponse stays null.
*/
response = (HttpWebResponse)request.GetResponse();
statuscode = (int)response.StatusCode;
}
catch (WebException ex)
{
statuscode = (int)((HttpWebResponse)ex.Response).StatusCode;
}
return new ApiResponse(statuscode, null, new String[] { apiPath });
});
}
其他信息:
在使用异步并等待之前,我尝试使用BackgroundWorker解决此问题。在这种情况下,我有一个类似的问题:有时上一个POST的backgroundWorker仍然很忙,并且该程序无法启动一个新的(死锁)。
Before doing this with async and await I tried to solve this issue by using a BackgroundWorker. In this case I had a similar problem: Sometimes the backgroundWorker of the previous POST was still busy and the program wasn't able to start a new one (deadlock).
此外,我发现了一些有趣的东西:
Furthermore I have found something interesting: async await Task null reference exception
- 但是在这种情况下,它是一个ASP.NET应用程序。我正在开发Windows桌面客户端-而不是服务器应用程序。
谢谢您的帮助!
推荐答案
在您的catch块中,您有(HttpWebResponse)ex.Response
。
In your catch block you have (HttpWebResponse)ex.Response
.
来自:
所以您的问题是服务器没有响应,并且错误处理出错。
但是不要解决此问题,请查找HttpClient并编写一个新的 async Task<>。 DoPost(...)
。
So your problem is a non-responsive server and an error in your error handling.
But don't fix this, look up the HttpClient and write a new async Task<> DoPost(...)
.
这篇关于HttpWebRequest随机失败并异步并等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!