including login credentials with a WebRequest

后端 未结 1 2261
长发绾君心
长发绾君心 2021-01-06 15:57

I am trying to \"behind the scenes\" log myself into a website, from the VB code behind my ASP.NET website. But I am dumbfounded as to how to do this.

As far as I k

1条回答
  •  一整个雨季
    2021-01-06 16:18

    If the client site uses basic authentication you can add credentials like this:

    WebRequest myReq = WebRequest.Create(url);
    CredentialCache mycache = new CredentialCache();
    mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));   
    myReq.Credentials = mycache;
    

    If it uses form login, you can use Fiddler to sniff the data posted on a login, and perform the same request from a HttpWebRequest object. You might want to handle cookies as well if you have to perform multiple requests with the logged in user.

    Reference:

    • Cookies: Automatic Cookie Handling C#/.NET HttpWebRequest+HttpWebResponse
    • Cookies and POST: HttpWebRequest POST and Cookies
    • Download class: Characters in string changed after downloading HTML from the internet

    0 讨论(0)
提交回复
热议问题