Http Post
-
Hi, I am working with the HTTP Post method to log the user into my framework. But when I run the page it does not redirect to the Page given in URL. What I am doing wrong? Following is my code: string strResponse; string strPost; protected void Page_Load(object sender, EventArgs e) { strPost = "txtUserName=abc&txtPassword=123"; UTF8Encoding objUTFEncode = new UTF8Encoding(); byte[] arrRequest = null; Stream objStreamReq; StreamReader objStreamRes; HttpWebRequest objHttpRequest; HttpWebResponse objHttpResponse; Uri objUri = new Uri("http://www.mysite.com"); objHttpRequest = HttpWebRequest.Create(objUri) as HttpWebRequest; objHttpRequest.KeepAlive = false; objHttpRequest.Method = "POST"; objHttpRequest.ContentType = "application/x-www-form-urlencoded"; arrRequest = objUTFEncode.GetBytes(strPost); objHttpRequest.ContentLength = arrRequest.Length; objStreamReq = objHttpRequest.GetRequestStream(); objStreamReq.Write(arrRequest, 0, arrRequest.Length); objStreamReq.Close(); CookieContainer cookieContainer = new CookieContainer(); objHttpRequest.CookieContainer = cookieContainer; //Get response objHttpResponse = objHttpRequest.GetResponse() as HttpWebResponse; objStreamRes = new StreamReader(objHttpResponse.GetResponseStream(), Encoding.ASCII); strResponse = objStreamRes.ReadToEnd(); objStreamRes.Close(); } Thanks
-
Hi, I am working with the HTTP Post method to log the user into my framework. But when I run the page it does not redirect to the Page given in URL. What I am doing wrong? Following is my code: string strResponse; string strPost; protected void Page_Load(object sender, EventArgs e) { strPost = "txtUserName=abc&txtPassword=123"; UTF8Encoding objUTFEncode = new UTF8Encoding(); byte[] arrRequest = null; Stream objStreamReq; StreamReader objStreamRes; HttpWebRequest objHttpRequest; HttpWebResponse objHttpResponse; Uri objUri = new Uri("http://www.mysite.com"); objHttpRequest = HttpWebRequest.Create(objUri) as HttpWebRequest; objHttpRequest.KeepAlive = false; objHttpRequest.Method = "POST"; objHttpRequest.ContentType = "application/x-www-form-urlencoded"; arrRequest = objUTFEncode.GetBytes(strPost); objHttpRequest.ContentLength = arrRequest.Length; objStreamReq = objHttpRequest.GetRequestStream(); objStreamReq.Write(arrRequest, 0, arrRequest.Length); objStreamReq.Close(); CookieContainer cookieContainer = new CookieContainer(); objHttpRequest.CookieContainer = cookieContainer; //Get response objHttpResponse = objHttpRequest.GetResponse() as HttpWebResponse; objStreamRes = new StreamReader(objHttpResponse.GetResponseStream(), Encoding.ASCII); strResponse = objStreamRes.ReadToEnd(); objStreamRes.Close(); } Thanks
Dot-Net-Dev wrote:
What I am doing wrong?
First, you are not following the guidelines for posting here. Use the pre tags to format any code you post. Of course it's not redirecting because you are not telling it to. All you are doing is writting, or attemtpting to, the content of the web request to the output stream of the same page. This is NOT the way to a redirect. Use Response.Redirect. This is also not the way to authenticate users. I suggst you read up on forms based authentication.
I know the language. I've read a book. - _Madmatt