2010-12-22 46 views
0

我写在C#(Windows窗体)一类C#持久的WebClient

这是一个WebClient类我故意在一些网站和登录和导航使用的。

下面是完整的类pastebin.com(类有197行,所以我只是用引擎收录。很抱歉,如果我做了一个有点困难供您阅读类,也低于这个帖子)

的问题是,我不知道为什么它不是持久的..我可以登录,但是当我导航到其他页面(不离开域)时,我被扔回登录页面。

你能帮我解决这个问题吗?

虽然有一个问题,但我试图连接的网站是“HTTPS”协议。我还没有对普通的HTTP进行测试。

预先感谢您。

/* 
* Web Client v1.2 
* --------------- 
* Date: 12/17/2010 
* author: Jayson Ragasa 
*/ 

using System; 
using System.Collections; 
using System.Collections.Specialized; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 
using System.Net; 
using System.Web; 

namespace Nullstring.Modules.WebClient 
{ 
    public class WebClientLibrary 
    { 
     #region vars 

     string _method = string.Empty; 

     ArrayList _params; 
     CookieContainer cookieko; 
     HttpWebRequest req = null; 
     HttpWebResponse resp = null; 
     Uri uri = null; 

     #endregion 

     #region properties 
     public string Method 
     { 
      set { _method = value; } 
     } 
     #endregion 

     #region constructor 
     public WebClientLibrary() 
     { 
      _method = "GET"; 
      _params = new ArrayList(); 
      cookieko = new CookieContainer(); 
     } 
     #endregion 

     #region methods 
     public void ClearParameter() 
     { 
      _params.Clear(); 
     } 

     public void AddParameter(string key, string value) 
     { 
      _params.Add(string.Format("{0}={1}", WebTools.URLEncodeString(key), WebTools.URLEncodeString(value))); 
     } 

     public string GetResponse(string URL) 
     { 
      StringBuilder response = new StringBuilder(); 

      #region create web request 
      { 
       uri = new Uri(URL); 
       req = (HttpWebRequest)WebRequest.Create(URL); 
       req.Method = "GET"; 
       req.GetLifetimeService(); 
      } 
      #endregion 

      #region get web response 
      { 
       resp = (HttpWebResponse)req.GetResponse(); 
       Stream resStream = resp.GetResponseStream(); 

       int bytesReceived = 0; 
       string tempString = null; 
       int count = 0; 
       byte[] buf = new byte[8192]; 

       do 
       { 
        count = resStream.Read(buf, 0, buf.Length); 

        if (count != 0) 
        { 
         bytesReceived += count; 
         tempString = Encoding.UTF8.GetString(buf, 0, count); 
         response.Append(tempString); 
        } 
       } 
       while (count > 0); 
      } 
      #endregion 

      return response.ToString(); 
     } 

     public string GetResponse(string URL, bool HasParams) 
     { 
      StringBuilder response = new StringBuilder(); 

      #region create web request 
      { 
       uri = new Uri(URL); 

       req = (HttpWebRequest)WebRequest.Create(URL); 
       req.MaximumAutomaticRedirections = 20; 
       req.AllowAutoRedirect = true; 

       req.Method = this._method; 
       req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
       req.KeepAlive = true; 

       req.CookieContainer = this.cookieko; 
       req.UserAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10"; 
      } 
      #endregion 

      #region build post data 
      { 
       if (HasParams) 
       { 
        if (this._method.ToUpper() == "POST") 
        { 
         string Parameters = String.Join("&", (String[])this._params.ToArray(typeof(string))); 

         UTF8Encoding encoding = new UTF8Encoding(); 
         byte[] loginDataBytes = encoding.GetBytes(Parameters); 

         req.ContentType = "application/x-www-form-urlencoded"; 
         req.ContentLength = loginDataBytes.Length; 

         Stream stream = req.GetRequestStream(); 
         stream.Write(loginDataBytes, 0, loginDataBytes.Length); 
         stream.Close(); 
        } 
       } 
      } 
      #endregion 

      #region get web response 
      { 
       resp = (HttpWebResponse)req.GetResponse(); 
       Stream resStream = resp.GetResponseStream(); 

       int bytesReceived = 0; 
       string tempString = null; 
       int count = 0; 
       byte[] buf = new byte[8192]; 

       do 
       { 
        count = resStream.Read(buf, 0, buf.Length); 

        if (count != 0) 
        { 
         bytesReceived += count; 
         tempString = Encoding.UTF8.GetString(buf, 0, count); 
         response.Append(tempString); 
        } 
       } 
       while (count > 0); 
      } 
      #endregion 

      return response.ToString(); 
     } 
     #endregion 
    } 

    public class WebTools 
    { 
     public static string EncodeString(string str) 
     { 
      return HttpUtility.HtmlEncode(str); 
     } 

     public static string DecodeString(string str) 
     { 
      return HttpUtility.HtmlDecode(str); 
     } 

     public static string URLEncodeString(string str) 
     { 
      return HttpUtility.UrlEncode(str); 
     } 

     public static string URLDecodeString(string str) 
     { 
      return HttpUtility.UrlDecode(str); 
     } 
    } 
} 

UPDATE年12月22
的GetResponse超载

public string GetResponse(string URL) 
{ 
    StringBuilder response = new StringBuilder(); 

    #region create web request 
    { 
     //uri = new Uri(URL); 
     req = (HttpWebRequest)WebRequest.Create(URL); 
     req.Method = "GET"; 
     req.CookieContainer = this.cookieko; 
    } 
    #endregion 

    #region get web response 
    { 
     resp = (HttpWebResponse)req.GetResponse(); 
     Stream resStream = resp.GetResponseStream(); 

     int bytesReceived = 0; 
     string tempString = null; 
     int count = 0; 
     byte[] buf = new byte[8192]; 

     do 
     { 
      count = resStream.Read(buf, 0, buf.Length); 

      if (count != 0) 
      { 
       bytesReceived += count; 
       tempString = Encoding.UTF8.GetString(buf, 0, count); 
       response.Append(tempString); 
      } 
     } 
     while (count > 0); 
    } 
    #endregion 

    return response.ToString(); 
}

但我仍得到了后仰到登录页面。

更新:12月23日
我试着列出饼干和这里就是我得到

首先,我必须登录到Web窗体,这我有这个Cookie JSESSIONID = 368C0AC47305282CBCE7A566567D2942

然后我导航到另一个页面(但在同一个域)我有一个不同的库克? JSESSIONID = 9FA2D64DA7669155B9120790B40A592C

出了什么问题?我使用更新的代码去年12月22日

+0

你应该使用`StreamReader`。你的代码会扼杀多字节字符。另外,并非所有页面都是UTF8;你需要检查响应编码。 – SLaks 2010-12-22 15:24:11

+0

顺便说一句,如果你告诉我们你正在试图反对的URL是什么,我们可能会更好地帮助你。 – 2010-12-22 17:10:22

回答

1

您需要在第一个GetResponse过载中使用CookieContainer。

1

如果使用GetResponse(字符串URL)覆盖导航到第二页,它不会将cookie容器附加到请求,因此来自登录请求的会话cookie(通常包含auth令牌)不是传递给服务器。因此,服务器不知道您已经登录。

更改您的代码以将cookie容器附加到所有请求,并且它应该工作。

或者,查看Web服务器是否接受来自无Cookie客户端的登录(通过在URL参数中发送会话信息)并更改您的代码以使用该方法。