Microsoft Developer Network >
Forums Home
>
Visual Studio Express Editions Forums
>
Visual C# Express Edition
>
using HttpWebRequest to login to a website
using HttpWebRequest to login to a website
- What's the best (& easiest) way of using HttpWebRequest to access certain website (login).Thanks!
Answers
- Hi,
Hope this is what you are looking for.
Encoding iso = Encoding.GetEncoding("iso-8859-9"); HttpWebRequest req = (HttpWebRequest)(WebRequest.Create("http://i2.social.microsoft.com/Image.avatr?size=Large&user=Tamer%20Oz&id=b86704f4-ef5c-4a1b-bcb3-dc1509ed45a1")); req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)"; req.Method = "GET"; req.KeepAlive = false; req.Timeout = 1000000; req.Proxy = WebProxy.GetDefaultProxy(); req.Proxy.Credentials = CredentialCache.DefaultCredentials; HttpWebResponse res = ((HttpWebResponse)req.GetResponse()); StreamReader read = new StreamReader(res.GetResponseStream(), iso); StreamWriter write = new StreamWriter(@"C:\users\tamer\desktop\b.jpg", false, iso); write.Write(read.ReadToEnd()); read.Close(); write.Close();- Marked As Answer byRedRose2009 Monday, November 02, 2009 8:14 PM
All Replies
- The best way to access a login site is using WebBrowser control.
Otherwise if you dont have any other option then using HttpWebRequest. I recomend you to watch http requests and responses with a tool like ieHttpHeaders(http://www.blunck.se/iehttpheaders/iehttpheaders.html) Then you can make your requests. But you'll have to manage cookies that sent with header. Viewstate information vs vs. I mean when te response comes you will have to store viewstate information and send it back at your request.
Here are some threads and some documentation about WebBrowser Control:
http://msdn.microsoft.com/en-us/library/2te2y1x6.aspx
http://msdn.microsoft.com/en-us/library/ms171711.aspx
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/f13f0b41-98c9-456d-8caf-1f274bd3d7bb
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/acf8ec6a-0f64-48ce-8e17-53c005123562
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/acf8ec6a-0f64-48ce-8e17-53c005123562
and a sample code for httprequest.
public class Client : WebClient { public void setDefaultHeaders() { this.Proxy = WebProxy.GetDefaultProxy(); this.Proxy.Credentials = CredentialCache.DefaultCredentials; this.Headers.Clear(); this.Headers.Add("Accept", @"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"); this.Headers.Add("Accept-Language", "tr"); this.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); this.Headers.Add("UA-CPU", "x86"); this.Headers.Add("Cache-Control", "no-cache"); this.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"); } } public class Request { string _postData; string _postUrl; byte[] _postByteArray; byte[] _responseArray; string _response; string _jSessionId; public string JSessionId { get { return _jSessionId; } set { _jSessionId = value; } } public byte[] PostByteArray { get { return _postByteArray; } set { _postByteArray = value; } } public byte[] ResponseArray { get { return _responseArray; } set { _responseArray = value; } } public string Response { get { return _response; } set { _response = value; } } public string PostData { get { return _postData; } set { _postData = value; } } public string PostUrl { get { return _postUrl; } set { _postUrl = value; } } public void getResponse() { } } public class Helper { public void getResponse(Request parRequest, Client parClient) { try { Encoding iso = Encoding.GetEncoding("iso-8859-9"); if (parRequest.PostData == "") { HttpWebRequest req = (HttpWebRequest)(WebRequest.Create(parRequest.PostUrl)); req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)"; req.Method = "GET"; req.KeepAlive = false; req.Timeout = 1000000; req.Proxy = WebProxy.GetDefaultProxy(); req.Proxy.Credentials = CredentialCache.DefaultCredentials; HttpWebResponse res = ((HttpWebResponse)req.GetResponse()); StreamReader sr = new StreamReader(res.GetResponseStream(),iso); parRequest.Response = sr.ReadToEnd(); } else { parRequest.PostByteArray = Encoding.UTF8.GetBytes(parRequest.PostData); parRequest.ResponseArray = parClient.UploadData(parRequest.PostUrl, "POST", parRequest.PostByteArray); parRequest.Response = iso.GetString(parRequest.ResponseArray); } } catch (Exception Ex) { } } public void getResponse(Request parRequest, Client parClient,bool usePost) { try { Encoding iso = Encoding.GetEncoding("iso-8859-9"); parRequest.PostByteArray = Encoding.UTF8.GetBytes(parRequest.PostData); parRequest.ResponseArray = parClient.UploadData(parRequest.PostUrl, "POST", parRequest.PostByteArray); parRequest.Response = iso.GetString(parRequest.ResponseArray); } catch (Exception Ex) { } } } public class Sample { Request insRequest = new Request(); Helper insHelper = new Helper(); Client insClient = new Client(); private void CallResponse() { insHelper.getResponse(insRequest, insClient); } private void CallResponse(bool usePost) { insHelper.getResponse(insRequest, insClient,true); } int queryCount = 0; string sessionId = ""; string userName = ""; string password = ""; public void Login() { insClient.setDefaultHeaders(); insRequest.PostData = ""; insRequest.PostUrl = "http://address"; CallResponse(true); sessionId = insClient.ResponseHeaders["Set-Cookie"]; insClient.setDefaultHeaders(); insClient.Headers.Add("Cookie", sessionId); insRequest.PostData = "userCode=" + userName + "&userPass=" + password + "&sendButton.x=0&sendButton.y=0"; insRequest.PostUrl = "http://address"; CallResponse(); } } - Thanks Tamer, I still haven't used your code for login to an external website but I am working on something similar at the moment. Right now all I want to do is call for URL that will process something (no need for login). I have to use HttpWebRequest. Basically this URL will produce an image which I have to grab and store it inside certain folder on my hard drive. Any guidelines?Thanks a lot! :))
- Hi,
Hope this is what you are looking for.
Encoding iso = Encoding.GetEncoding("iso-8859-9"); HttpWebRequest req = (HttpWebRequest)(WebRequest.Create("http://i2.social.microsoft.com/Image.avatr?size=Large&user=Tamer%20Oz&id=b86704f4-ef5c-4a1b-bcb3-dc1509ed45a1")); req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)"; req.Method = "GET"; req.KeepAlive = false; req.Timeout = 1000000; req.Proxy = WebProxy.GetDefaultProxy(); req.Proxy.Credentials = CredentialCache.DefaultCredentials; HttpWebResponse res = ((HttpWebResponse)req.GetResponse()); StreamReader read = new StreamReader(res.GetResponseStream(), iso); StreamWriter write = new StreamWriter(@"C:\users\tamer\desktop\b.jpg", false, iso); write.Write(read.ReadToEnd()); read.Close(); write.Close();- Marked As Answer byRedRose2009 Monday, November 02, 2009 8:14 PM
- it works like a charm! :)) One question, why do you use Encoding iso = Encoding.GetEncoding("iso-8859-9");?
- It's encoding for Turkish normally I use this code to get the source of a page.
You can use Encoding.Default

