2010-03-16 85 views
2

如何使用c#中的webrequst和webresponse帮助登录https网站。如何使用webrequest和响应登录https网站

这里是代码

public string postFormData(Uri formActionUrl, string postData) 
    { 
     gRequest = (HttpWebRequest)WebRequest.Create(formActionUrl); 
     gRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4"; 
     gRequest.CookieContainer = new CookieContainer(); 
     gRequest.Method = "POST"; 
     gRequest.Accept = " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8, */*"; 
     gRequest.KeepAlive = true; 
     gRequest.ContentType = @"text/html; charset=iso-8859-1"; 

     #region CookieManagement 
     if (this.gCookies != null && this.gCookies.Count > 0) 
     { 
      gRequest.CookieContainer.Add(gCookies); 
     } 

     //logic to postdata to the form 
     string postdata = string.Format(postData); 
     byte[] postBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes(postData); 
     gRequest.ContentLength = postBuffer.Length; 
     Stream postDataStream = gRequest.GetRequestStream(); 
     postDataStream.Write(postBuffer, 0, postBuffer.Length); 
     postDataStream.Close(); 
     //post data logic ends 

     //Get Response for this request url 
     gResponse = (HttpWebResponse)gRequest.GetResponse(); 



     //check if the status code is http 200 or http ok 
     if (gResponse.StatusCode == HttpStatusCode.OK) 
     { 
      //get all the cookies from the current request and add them to the response object cookies 
      gResponse.Cookies = gRequest.CookieContainer.GetCookies(gRequest.RequestUri); 
      //check if response object has any cookies or not 
      if (gResponse.Cookies.Count > 0) 
      { 
       //check if this is the first request/response, if this is the response of first request gCookies 
       //will be null 
       if (this.gCookies == null) 
       { 
        gCookies = gResponse.Cookies; 
       } 
       else 
       { 
        foreach (Cookie oRespCookie in gResponse.Cookies) 
        { 
         bool bMatch = false; 
         foreach (Cookie oReqCookie in this.gCookies) 
         { 
          if (oReqCookie.Name == oRespCookie.Name) 
          { 
           oReqCookie.Value = oRespCookie.Name; 
           bMatch = true; 
           break; // 
          } 
         } 
         if (!bMatch) 
          this.gCookies.Add(oRespCookie); 
        } 
       } 
      } 
     #endregion 



      StreamReader reader = new StreamReader(gResponse.GetResponseStream()); 
      string responseString = reader.ReadToEnd(); 
      reader.Close(); 
      //Console.Write("Response String:" + responseString); 
      return responseString; 
     } 
     else 
     { 
      return "Error in posting data"; 
     } 
    } 

// calling the above function 

httphelper.postFormData(new Uri("https://login.yahoo.com/config/login?.done=http://answers.yahoo.com%2f&.src=knowsrch&.intl=us"), ".tries=1&.src=knowsrch&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.bypass=&.partner=&.u=0b440p15q1nmb&.v=0&.challenge=Rt_fM1duQiNDnI5SrzAY_GETpNTL&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.chkP=Y&.done=http%3A%2F%2Fanswers.yahoo.com%2F&.pd=knowsrch_ver%3D0%26c%3D%26ivt%3D%26sg%3D&login=xyz&passwd=xyz&.save=Sign+In"); 
+1

请详细说明。你已经做了什么? – naivists 2010-03-16 11:36:16

回答

0

为了什么? Watin适合测试等,并且很容易做到基本的屏幕抓取。为什么重新发明轮子,如果你不需要。

1

您需要了解身份验证如何适用于您正在使用的网站。

这可能是通过cookie,特殊标题,隐藏字段或其他东西。

  1. 火起来像Fiddler的工具,看看网络流量是登录时像什么,它是如何不同于没有被记录在
  2. 重新创建的WebRequest和WebResponse这个逻辑。

请参阅this的答案SO问题(HttpRequest:通过AuthLogin)。