2015-12-29 118 views
1

我想用C#登录本网站:这是我的尝试,但它将我发送到第一页。也不回我的下一个页面,应该登录后可以看到,请大家帮我解决此问题:使用C#以编程方式登录网站#

string formParams = 
string.Format("mail={0}&password={1}", [email protected]", "admin"); 
     string cookieHeader; 
     WebRequest req = WebRequest.Create("http://muslimgowns.com/dashboard/login/public_login"); 
      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); 
      } 
      WebResponse resp = req.GetResponse(); 
      cookieHeader = resp.Headers["Set-cookie"]; 
      using (StreamReader sr = new StreamReader(resp.GetResponseStream())) 
      { 
       string pageSource = sr.ReadToEnd(); 
       File.AppendAllText("first.txt", pageSource); 
      } 

      string pageSource1; 
      string getUrl = "http://muslimgowns.com/dashboard/home"; 
      WebRequest getRequest = WebRequest.Create(getUrl); 
      getRequest.Headers.Add("Cookie", cookieHeader); 
      WebResponse getResponse = getRequest.GetResponse(); 
      using (StreamReader sr = new StreamReader(getResponse.GetResponseStream())) 
      { 
       pageSource1 = sr.ReadToEnd(); 
       File.AppendAllText("second.txt", pageSource1); 
      } 
     } 
+0

你使用例如Fiddler或TamperData(Firefox)查看在主页显示之前实际发送到服务器的请求?你也许想看看[这](http://stackoverflow.com/questions/31585606/checking-if-website-is-working-and-login/31617770#31617770)的答案。 – Streamline

回答

1

好像第一GET请求public_login回报饼干的量,并与凭证POST请求之后必须送到login_access而不是public_login

使用的HttpWebRequest代替WebRequest,并通过设置cookie容器有帮助,而实际上服务器HTTP 302 Redirect响应POST请求和HttpWebRequest自动跟随这重定向并下载仪表板。

请始终使用像Fiddler或Wireshark或网络监视器或浏览器的开发人员工具的http跟踪工具来查看收到的内容(cookie,标题等)以及发回的内容。这就是我得到这一切的方式。

这里是修复:

string formParams = string.Format("mail={0}&password={1}", "[email protected]", "admin"); 

CookieContainer cookieContainer = new CookieContainer(); 

HttpWebRequest req = WebRequest.CreateHttp("http://muslimgowns.com/dashboard/login/public_login"); 
req.CookieContainer = cookieContainer; 
req.GetResponse(); // This is just to get the initial cookies returned by the public_login 

req = WebRequest.CreateHttp("http://muslimgowns.com/dashboard/login/login_access"); 
req.CookieContainer = cookieContainer; // Set the cookie container which contains the cookies returned by the public_login 

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

WebResponse resp = req.GetResponse(); 

using (StreamReader sr = new StreamReader(resp.GetResponseStream())) 
{ 
    string pageSource = sr.ReadToEnd(); 
    File.AppendAllText("first.txt", pageSource); // Dashboard is returned. 
} 
+0

这是Oguz Ozgul的完美答案 –

+0

谢谢:)此答案的重要部分不是针对此特定情况的修复。我认为最重要的部分是建议使用Http分析器来查看请求的资源和请求/响应细节,这将有助于进一步开发 –

+0

是的,我试着Http分析器。我对此很陌生,也面临一些问题。你可以请指导我,我如何登录到fastundercar.com。以下网址我正在尝试。要登录:http://www.fastundercar.com/ThemeLanding/Default28.aspx和登录后:http://www.fastundercar.com/SiteManagement/UISettings/DisplayHtml.aspx 也有任何方法可以传递隐藏的字段与用户和密码信息。如果是,那么语法是什么。 预先感谢您 –