获取WebBrowser全cookie 和 httpWebRequest 异步获取页面数据

获取WebBrowser全cookie

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref int pcchCookieData, int dwFlags, object lpReserved);
//取出Cookie,当登录后才能取
private static string GetCookieString(string url)
{
// Determine the size of the cookie
int datasize = 256;
StringBuilder cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
{
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
return null;
}
return cookieData.ToString();
}

直接调用GetCookieString方法获取cookie

httpWebRequest 异步获取页面数据

private static ManualResetEvent allDone = new ManualResetEvent(false);

private const int BUFFER_SIZE = 1024;
public static void AsyncResponse(string url, CookieCollection cookieCollection)
{
// 从命令行获取 URI
Uri HttpSite = new Uri(url);

// 创建请求对象
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(HttpSite);
wreq.Method = "GET";

if (cookieCollection != null)
{
wreq.CookieContainer = new CookieContainer();
wreq.CookieContainer.Add(cookieCollection);
}
// 创建状态对象
RequestState rs = new RequestState();

// 将请求添加到状态,以便它可以被来回传递
rs.Request = wreq;

// 发出异步请求
IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(new AsyncCallback(RespCallback), rs);

// 将 ManualResetEvent 设置为 Wait,
// 以便在调用回调前,应用程序不退出
allDone.WaitOne();

}

private static void RespCallback(IAsyncResult ar)
{
// 从异步结果获取 RequestState 对象
RequestState rs = (RequestState)ar.AsyncState;

// 从 RequestState 获取 HttpWebRequest
HttpWebRequest req = rs.Request;

// 调用 EndGetResponse 生成 HttpWebResponse 对象
// 该对象来自上面发出的请求
HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(ar);

// 既然我们拥有了响应,就该从
// 响应流开始读取数据了
Stream ResponseStream = resp.GetResponseStream();

// 该读取操作也使用异步完成,所以我们
// 将要以 RequestState 存储流
rs.ResponseStream = ResponseStream;

// 请注意,rs.BufferRead 被传入到 BeginRead。
// 这是数据将被读入的位置。
IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
}

private static void ReadCallBack(IAsyncResult asyncResult)
{
// 从 asyncresult 获取 RequestState 对象
RequestState rs = (RequestState)asyncResult.AsyncState;

// 取出在 RespCallback 中设置的 ResponseStream
Stream responseStream = rs.ResponseStream;

// 此时 rs.BufferRead 中应该有一些数据。
// 读取操作将告诉我们那里是否有数据
int read = responseStream.EndRead(asyncResult);

if (read > 0)
{
// 准备 Char 数组缓冲区,用于向 Unicode 转换
Char[] charBuffer = new Char[BUFFER_SIZE];

// 将字节流转换为 Char 数组,然后转换为字符串
// len 显示多少字符被转换为 Unicode

int len = rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0);
String str = new String(charBuffer, 0, len);

// 将最近读取的数据追加到 RequestData stringbuilder 对象中,
// 该对象包含在 RequestState 中
rs.RequestData.Append(str);

// 现在发出另一个异步调用,读取更多的数据
// 请注意,将不断调用此过程,直到
// responseStream.EndRead 返回 -1
IAsyncResult ar = responseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
}
else
{
if (rs.RequestData.Length > 1)
{
// 所有数据都已被读取,因此将其显示到控制台
string strContent;
strContent = rs.RequestData.ToString();
Console.WriteLine(strContent);
}

// 关闭响应流
responseStream.Close();

// 设置 ManualResetEvent,以便主线程可以退出
allDone.Set();
}
return;
}

public class RequestState
{
const int BUFFER_SIZE = 1024;
public StringBuilder RequestData;
public byte[] BufferRead;
public HttpWebRequest Request;
public Stream ResponseStream;
// 创建适当编码类型的解码器
public Decoder StreamDecode = Encoding.UTF8.GetDecoder();

public RequestState()
{
BufferRead = new byte[BUFFER_SIZE];
RequestData = new StringBuilder("");
Request = null;
ResponseStream = null;
}
}

上一篇:Struts1.x下使用jquery的Ajax获取后台数据


下一篇:keeplived工作原理及配置