0
我用下面的代码在C#中执行异步HTTP请求。C#4.0中的异步HTTP POST
private static Task GetUrl(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent =
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36";
request.Accept = "text/html";
return Task
.Factory
.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, url)
.ContinueWith(t =>
{
if (t.IsCompleted)
{
using (var stream = t.Result.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
//Console.WriteLine("-- Successfully downloaded {0} --", t.AsyncState);
//Console.WriteLine(reader.ReadToEnd());
}
}
}
else if (t.IsFaulted)
{
Console.WriteLine("There was an error downloading {0} - {1}", t.AsyncState, t.Exception);
}
});
}
但是我不知道我应该如何修改上面的代码来支持HTTP POST。任何帮助表示赞赏!
我特别想知道我应该如何添加BeginGetRequestStream和EndGetRequestStream到当前的功能...
只要搜索“的WebRequest POST “,这很简单,并且[这里是](http://msdn.microsoft.com/en-us/library/debx8sh9.aspx)一个链接......它与你的其他代码无关,你只需要以不同的方式配置您的WebRequest。 –
请参阅: http://stackoverflow.com/questions/18513907/how-to-make-multiple-post-request-using-httpwebrequest-in-silverlight-4-and-net – Nostradamus