Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HttpWebRequest、c#和Https_C#_Ssl_Https_Httpwebrequest - Fatal编程技术网

HttpWebRequest、c#和Https

HttpWebRequest、c#和Https,c#,ssl,https,httpwebrequest,C#,Ssl,Https,Httpwebrequest,我尝试了很多方法以编程方式登录https网站,但我遇到了一些问题。每次我收到一个错误,说明我的登录名和密码不正确。我确信它们是正确的,因为我可以使用相同的凭据通过浏览器登录到该站点 失败代码 您正在尝试发布某些内容(我看不出,是什么,来自您的代码),但没有发布凭据。我猜您的网页会显示一个web表单,您可以在其中输入用户名(电子邮件地址?)和密码。然后浏览器发布这个表单。因此,您需要复制浏览器行为—对表单内容进行编码并在post请求中发送它们。使用一些流行浏览器的站长开发工具来查看客户端浏览器到底

我尝试了很多方法以编程方式登录https网站,但我遇到了一些问题。每次我收到一个错误,说明我的登录名和密码不正确。我确信它们是正确的,因为我可以使用相同的凭据通过浏览器登录到该站点

失败代码
您正在尝试发布某些内容(我看不出,是什么,来自您的代码),但没有发布凭据。我猜您的网页会显示一个web表单,您可以在其中输入用户名(电子邮件地址?)和密码。然后浏览器发布这个表单。因此,您需要复制浏览器行为—对表单内容进行编码并在post请求中发送它们。使用一些流行浏览器的站长开发工具来查看客户端浏览器到底向服务器发送了什么,以及它如何对表单数据进行编码。接下来,您的请求很可能需要特殊的cookies,您可以通过访问另一个页面(例如登录页面)来收集这些cookies。发送预设cookie(就像您在注释代码中所做的那样)对大多数网站都不起作用

换言之,适当的机制是:

  • 获取登录网页
  • 收集饼干
  • 发布表单数据并在请求中传递收集的cookie
  • 收集其他cookie,这些cookie可能在登录后发送
    该站点使用HTTPPOST进行登录,并且不在URL中发送用户名和密码

    正确的登录URL是
    https://www.majesticseo.com/account/login

    您需要创建一个要发布的数据字符串,将其转换为字节数组,设置内容长度,然后执行请求。发送内容长度非常重要。没有它,这个职位就无法运作

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.majesticseo.com/account/login?EmailAddress=myemail&Password=mypass&RememberMe=1");
    
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";
            request.Referer = "https://www.majesticseo.com/account/login";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8";
            request.UnsafeAuthenticatedConnectionSharing = true;
            request.Method = "POST";
            request.KeepAlive = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.AllowAutoRedirect = true;
    
            // the post string for login form
            string postData = "redirect=&EmailAddress=EMAIL&Password=PASS";
            byte[] postBytes = System.Text.Encoding.ASCII.GetBytes(postData);
    
            request.ContentLength = postBytes.Length;
    
            System.IO.Stream str = request.GetRequestStream();
    
            str.Write(postBytes, 0, postBytes.Length);
    
            str.Close();
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
            System.IO.Stream stream = response.GetResponseStream();
    
    
            string html = new System.IO.StreamReader(stream).ReadToEnd();
    
            Console.WriteLine("" + html);
    

    您在代码中的何处设置凭据?所有注释掉的代码的目的是什么?这与问题有关吗?如果没有,删除它。很明显,你没有做基本的身份验证,所以我不能真正建议你需要设置哪些字段,因为我不知道这个网站是什么。另外,请格式化并清理这段代码,它的当前形式非常混乱。我曾尝试过这种方式,但这不起作用。第一个解决方案正在工作(以字节数组的形式发送登录数据)。谢谢。@jars好吧,如果不调试您的代码和检查您的特定站点,我认为任何人都无法在这里帮助您。所以你在这里运气不好。
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.majesticseo.com/account/login?EmailAddress=myemail&Password=mypass&RememberMe=1");
    
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";
            request.Referer = "https://www.majesticseo.com/account/login";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8";
            request.UnsafeAuthenticatedConnectionSharing = true;
            request.Method = "POST";
            request.KeepAlive = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.AllowAutoRedirect = true;
    
            // the post string for login form
            string postData = "redirect=&EmailAddress=EMAIL&Password=PASS";
            byte[] postBytes = System.Text.Encoding.ASCII.GetBytes(postData);
    
            request.ContentLength = postBytes.Length;
    
            System.IO.Stream str = request.GetRequestStream();
    
            str.Write(postBytes, 0, postBytes.Length);
    
            str.Close();
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
            System.IO.Stream stream = response.GetResponseStream();
    
    
            string html = new System.IO.StreamReader(stream).ReadToEnd();
    
            Console.WriteLine("" + html);