I have a method that hangs when declaring a responseObject using Task.Factory.FromAsync ()
private async Task<string> post(string url, string postdata) { var request = WebRequest.Create(new Uri(url)) as HttpWebRequest; request.Method = "POST";
It works fine if I manually run Begin / End methods, for example:
request.BeginGetRequestStream(async ar => { var requestStream = request.EndGetRequestStream(ar); using (var sw = new StreamWriter(requestStream)) { byte[] data = Encoding.UTF8.GetBytes(postdata); await requestStream.WriteAsync(data, 0, data.Length); } request.BeginGetResponse(async a => { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(a); var responseStream = response.GetResponseStream(); using (var sr = new StreamReader(responseStream)) { string received = await sr.ReadToEndAsync(); } }, null); }, null);
However, in this case, ReadToEndAsync () is run in a separate thread, and I want the method to return a result, which is not possible since the method returns before the BeginGetResponse callback completes.
At this moment, I think I'm doing this terribly wrong, and this is completely wrong, so any help would be greatly appreciated.
zi3guw
source share