17,741
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// .Net 内置 Cookie 不支持序列化, 所以需要一个 可序列化的 Cookie 实例
/// </summary>
[Serializable]
internal class HttpCookieInfo
{
private string m_Name = string.Empty;
private string m_Value = string.Empty;
private string m_Path = string.Empty;
private string m_Domain = string.Empty;
private DateTime m_Expires = DateTime.MinValue;
public string Name
{
get { return m_Name; }
set { m_Name = (value ?? string.Empty).Trim(); }
}
public string Value
{
get { return m_Value; }
set { m_Value = (value ?? string.Empty).Trim(); }
}
public string Path
{
get { return m_Path; }
set { m_Path = (value ?? string.Empty).Trim(); }
}
public string Domain
{
get { return m_Domain; }
set { m_Domain = (value ?? string.Empty).Trim(); }
}
public DateTime Expires
{
get { return m_Expires; }
set { m_Expires = value; }
}
public override string ToString()
{
return string.Format("{0}={1}", this.Name, this.Value);
}
public static string JoinCookie(List<HttpCookieInfo> listCookie)
{
if (listCookie == null || listCookie.Count <= 0) return string.Empty;
HttpSimpDict<string, HttpCookieInfo> hashTemp = new HttpSimpDict<string, HttpCookieInfo>();
foreach (HttpCookieInfo cookieInfo in listCookie)
if (cookieInfo != null) hashTemp[cookieInfo.Name] = cookieInfo;
List<HttpCookieInfo> listTemp = new List<HttpCookieInfo>(hashTemp.Values);
//string result = string.Join("; ", listTemp.Select(x => x.Express)); //N40String.Join("; ", N40IList.Select(listTemp, (x => x.Express)));
string result = string.Join("; ", listTemp.Select(x => string.Format("{0}={1}", x.Name, x.Value))); //N40String.Join("; ", N40IList.Select(listTemp, (x => string.Format("{0}={1}",x.Name,x.Value))));
return result;
}
}
private static object FileDeserialize(string path, bool withZip)
{
if (!System.IO.File.Exists(path)) return null;
try
{
BinaryFormatter myBf = new BinaryFormatter();
using (FileStream myFs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
Stream stream = withZip ? (Stream)new GZipStream(myFs, CompressionMode.Decompress) : (Stream)myFs;
object record = null;
try { record = myBf.Deserialize(stream); }
finally { stream.Close(); stream.Dispose(); }
return record;
}
}
catch (Exception exp)
{
//throw new Exception(string.Format("读取磁盘序列化失败: {0}", exp.Message));
return null;
}
}
private static bool FileSerialize(string path, object record, bool withZip)
{
//if (!System.IO.File.Exists(path)) return false;
try
{
System.IO.Directory.CreateDirectory(path);
BinaryFormatter myBf = new BinaryFormatter();
using (FileStream myFs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
{
Stream stream = withZip ? (Stream)new GZipStream(myFs, CompressionMode.Compress) : (Stream)myFs;
try { myBf.Serialize(stream, record); }
finally { stream.Close(); stream.Dispose(); }
}
return true;
}
catch (Exception exp)
{
//throw new Exception(string.Format("写入磁盘序列化失败: {0}", exp.Message));
return false;
}
}
//如果 设置了 CookieContainer 就不能用 Headers 设置Cookie
//将 磁盘文件 反序列化 成 Cookie对象集合
List<HttpCookieInfo> listCookie = (List<HttpCookieInfo>)FileDeserialize(@"D:\AAAA.cookie");
//string joinCookie = HttpCookieInfo.JoinCookie(listCookie);
//if (!String.IsNullOrWhiteSpace(joinCookie))
// request.Headers[HttpRequestHeader.Cookie] = joinCookie;
//将 Cookie对象集合 添加到 HttpWebRequest 请求中
if (listCookie != null && listCookie.Count >= 1)
{
foreach (HttpCookieInfo cookieInfo in listCookie)
{
Cookie cookie = new Cookie(cookieInfo.Name, cookieInfo.Value, cookieInfo.Path, cookieInfo.Domain);
cookie.Expires = cookieInfo.Expires;
request.CookieContainer.Add(cookie);
}
}