Click here to Skip to main content
15,501,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to login to the password secures website using web request.The following is my code.Am unable to login to the site.Please help me out
Posted
Updated 17-Dec-12 22:23pm
v3
Comments
Sandeep Mewara 12-Jun-12 3:05am     CRLF
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific. Use the "Improve question" link to edit your question and provide better information.

1 solution

Try to close the request stream before retrieving the response.
You can accomplish this by using "using" blocks:

C#
public string login(string url, string postData)
{
    byte[] requestData = Encoding.UTF8.GetBytes(postData);
    
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ContentLength = requestData .Length;
 
    using (Stream requestStream = webRequest.GetRequestStream())
    {
        webpageStream.Write(requestData, 0, requestData.Length);
    } //this will close the requestStream automatically

    WebResponse webpageResponse = webRequest.GetResponse();
 
    using (responseReader = new StreamReader(webpageResponse.GetResponseStream()))
    {
        return webpageReader.ReadToEnd();
    } //the reader will close the underlaying stream too
}    


(Besides, I've done some style improvements you should adopt, the code will be a far more readable)

For more infos, see a Tutorial on MSDN.[^]

And:
Response.Write(responseFromServer);

Never do that!
What is Response? (A rhetorical question)
This is called bad side effects...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900