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