lundi 28 juin 2021

Bad request when trying to login to website using C#

Good morning,

I am trying to login into this website "https://ift.tt/3hiAWCe" and later get some information of a bunch of products (which are locked behind the Login) on this website.

This is the code I wrote with help of the internet.

string username = "email";
string password = "password";
string formUrl = "https://www.da-shi.de/account";
string formParams = string.Format("email_address={0}&password={1}", username, password);
string cookieHeader;

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
    os.Write(bytes, 0, bytes.Length);
}

using (WebResponse resp = req.GetResponse())
{
    cookieHeader = resp.Headers["Set-cookie"];
}

string SitemapURL = "https://www.da-shi.de/wasserpfeifen/shizu/alu/7579/shizu-shisha-light-922-black";
string pageSource = WebRequest_(cookieHeader, SitemapURL);

static string WebRequest_(string cookieHeader, string getUrl)
{
     string pageSource;

     WebRequest getRequest = WebRequest.Create(getUrl);
     getRequest.Headers.Add("Cookie", cookieHeader);
     WebResponse getResponse = getRequest.GetResponse();
     using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
     {
          pageSource = sr.ReadToEnd();
     }
     return (pageSource);
}

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(pageSource);

It returns the Error "Bad Request" and I dont know why...

What am I doing wrong?




Aucun commentaire:

Enregistrer un commentaire