问题
I used the code below to open a stream request to a youtube video, but it always return an exception "The remote server returned an error: NotFound". Then I tried to use Fiddler to detect the problem, and I saw that the WebClient auto set User-Agent field to NativeHost, not my User-Agent as below.
My code to send a request to youtube:
private static Task<string> HttpGet(string uri)
{
var task = new TaskCompletionSource<string>();
var web = new WebClient();
web.OpenReadCompleted += (sender, args) =>
{
if (args.Cancelled)
task.SetCanceled();
else if (args.Error != null)
task.SetException(args.Error);
else
{
//var bytes = args.Result.ReadToEnd();
byte[] bytes = new byte[] { };
using (MemoryStream memoryStream = new MemoryStream())
{
args.Result.CopyTo(memoryStream);
bytes = memoryStream.ToArray();
task.SetResult(Encoding.UTF8.GetString(bytes, 0, bytes.Length));
}
}
};
web.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
web.OpenReadAsync(new Uri(uri));
return task.Task;
}
Headers that were captured from Fiddler:
CONNECT www.youtube.com:443 HTTP/1.0
User-Agent: NativeHost
Host: www.youtube.com:443
Content-Length: 0
Connection: Keep-Alive
Pragma: no-cache
Please give me some advice. Many thanks
回答1:
You can use this code
using (WebClient web = new WebClient())
{
web.Headers["User-Agent"] =
"Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
"(compatible; MSIE 6.0; Windows NT 5.1; " +
".NET CLR 1.1.4322; .NET CLR 2.0.50727)";
}
回答2:
Try this for adding the User Agent
web.Headers.Add("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
回答3:
You can do this as well.
using (WebClient webClient = new WebClient())
{
webClient.Headers.Add("user-agent", "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Safari/537.36");
}
来源:https://stackoverflow.com/questions/22656049/how-to-set-user-agent-in-webclient