المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Setting the UserAgent for WebRequest



C# Programming
05-26-2009, 12:41 AM
I have tried searching and everything I find says to use "WebRequest.UserAgent" but this isnt a valid option in my solution to add. Am I doing something wrong? The script is pulling content from the webpage 100% correctly, I just want to be able to specify the user agent also.

byte[] data = System.Text.Encoding.ASCII.GetBytes(FormVariables);

System.IO.Stream os = null;
WebRequest request = null;
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;

request = WebRequest.Create(URL);

request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.ContentLength = data.Length;
request.Timeout = 420000;

os = request.GetRequestStream();
os.Write(data, 0, data.Length);
os.Close();

System.Net.WebResponse resp = null;
resp = request.GetResponse();

System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
strResponse = sr.ReadToEnd().Trim();