Does the .NET WebClient follow the Location header which should be sent together with a 201 Created
response? If yes, is there a way 开发者_C百科to disable it?
Additionally I'm looking for a way to retrieve the value of the Location header as I'd like to display the link of the created resource to the user.
Checked the .NET framework sourcecode and here's the answer: WebClient uses HttpWebRequest which uses the following logic for following redirects:
else if (AllowAutoRedirect && (
ResponseStatusCode==HttpStatusCode.Ambiguous || // 300
ResponseStatusCode==HttpStatusCode.Moved || // 301
ResponseStatusCode==HttpStatusCode.Redirect || // 302
ResponseStatusCode==HttpStatusCode.RedirectMethod || // 303
ResponseStatusCode==HttpStatusCode.RedirectKeepVerb )) // 307
{
The value of the Location header is in the ResponseHeaders
collection of the WebClient (or the event args when using async).
精彩评论