0
Reply

How to pass Login Information with HttpWebRequest ?

Kiran Suthar

Kiran Suthar

Dec 27 2005 5:45 AM
1.9k
Hello friends, I want to post some data using HttpWebRequest. In my case the server requires the login information so i need to provide login information along with request. I have login information in the form of Cookie collection. I have added it to the HttpWebRequest's cookiecollection, but it is not working. Please look at the following code. StreamReader srTemplate = new StreamReader(FilePath); string strTemplate = srTemplate.ReadToEnd(); string URL="my url"; string strQuerystring = "ID=" + HttpUtility.UrlEncode(ID1) + "&templateText=" + HttpUtility.UrlEncode(strTemplate) + "&submit=" + HttpUtility.UrlEncode("Save")+ "\r\n"; byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(strQuerystring); HttpWebRequest HWReq = (HttpWebRequest)WebRequest.Create(URL); HWReq.AllowWriteStreamBuffering = false; HWReq.ContentLength = Buffer.Length; HWReq.Timeout = (int)cbTimeout.SelectedItem * 1000; HWReq.UserAgent = "Mozilla/4.0 (compatible;MSIE 6.0b;Windows NT 5.0)"; HWReq.AllowAutoRedirect = true; HWReq.Method = "POST"; HWReq.ContentType = "application/x-www-form-urlencoded"; HWReq.CookieContainer = new CookieContainer(); HWReq.CookieContainer.Add(ccLogin); // ccLogin is the cookie collection which contains login details. Stream PostData = HWReq.GetRequestStream(); PostData.Write(Buffer, 0, Buffer.Length); PostData.Close(); HttpWebResponse HWResp = (HttpWebResponse)HWReq.GetResponse(); StreamReader ResponseStream = new StreamReader(HWResp.GetResponseStream()); string strRespStream = ResponseStream.ReadToEnd(); Here from the strRespStream I can identify that server is asking me to login. i.e. login information is not passed with the HttpWebRequest. Can any body help me how can i pass login details ?? I need to solve this Urgently Thanks & Rgds, Kiran Suthar.

Next Recommended Forum