how to implement login from code to other site (hidden login)

Locked how to implement login from code to other site (hidden login)

  • Tuesday, June 07, 2005 7:03 AM
     
     
    hi there ,
    i want to login to another site using httpWebreqwest & httpWebResponse classes & i used the falwing code :

    public string Crawl_UsingPost(string postData)
    {
    req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";
    req.Credentials =new NetworkCredential(username,password);
    req.ContentType = "text/html";


    req.Headers.Add("username",username);
    req.Headers.Add("password",password);
    req.PreAuthenticate=true;
    req.Pipelined=true;
    req.ContentLength= postData.Length;
    CookieContainer cookies = new CookieContainer();
    req.CookieContainer=cookies;

    req.KeepAlive=true;
    StreamWriter stOut = new StreamWriter(req.GetRequestStream(),System.Text.Encoding.ASCII);
    stOut.Write(postData);
    stOut.Close();

    webResponse=(HttpWebResponse)req.GetResponse();
    string responseString="";
    if (req.HaveResponse)
    {
    StreamReader stream = new StreamReader
    (webResponse.GetResponseStream());
    responseString = stream.ReadToEnd();
    stream.Close();
    webResponse.Close();
    }
    return responseString;
    }

    i will appreciate your reply thanks

All Replies

  • Tuesday, June 07, 2005 3:24 PM
    Moderator
     
     Answered
    You are using the correct approach.  To authenticate to a remote host all you need to do is add your credentials to the request:
       

    req.Credentials =new NetworkCredential(username,password);
     


    You can also add a CredentialCache to your request to specify different credentials for different remote hosts and authentication schemes:
       

    CredentialCache cache = new CredentialCache()
       cache.Add(new Uri("http://www.microsoft.com", "Digest", new NetworkCredential(username,password));
       req.Credentials = cache;

     


    Finally, if you want to just use the credentials of the current Windows user, you can just configure the request to use the default credentials:
       

    req.UseDefaultCredentials = true;
     


    I advise against putting the username and password in the headers.  This is insecure, and I don't think it is necessary.

    Daniel Roth
    System.Net
  • Wednesday, June 08, 2005 7:28 AM
     
     
    Thanks Daniel Roth,
    I do your changes but i still have the problem i can't login yet.

    I need more help, what I was missed in my code.

    Thanks.

  • Thursday, June 16, 2005 6:10 PM
    Moderator
     
     
    Whats the error/exception that you are seeing?