尝试使用 C# 登录网站时出现错误请求

时间:2021-06-28 07:26:45

标签: c# authentication web httpwebrequest

早上好,

我试图登录这个网站“https://www.da-shi.de/account”,后来在这个网站上获取了一堆产品的一些信息(被锁定在登录后)。

>

这是我在互联网的帮助下编写的代码。

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);

它返回错误“错误请求”,我不知道为什么...

我做错了什么?

0 个答案:

没有答案