Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 11-Jun-12 20:42pm
Edited 17-Dec-12 21:23pm
v3
Comments
Sandeep Mewara at 12-Jun-12 3:05am
   
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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try to close the request stream before retrieving the response.
You can accomplish this by using "using" blocks:
 
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...
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100