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

    問題

  • 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

    2005年6月7日 上午 07:03

解答

  • 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
    2005年6月7日 下午 03:24
    版主

所有回覆

  • 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
    2005年6月7日 下午 03:24
    版主
  • 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.

    2005年6月8日 上午 07:28
  • Whats the error/exception that you are seeing?
    2005年6月16日 下午 06:10
    版主