我之前問的
http://www.sufeinet.com/forum.ph ... ;tid=19731#lastpost
我後來就是用
webbrowser 認證成功後,將 cookie 給 CookieContainer
現在我想用 HttpHelper 去改寫
之前成功的代碼是
[C#] 纯文本查看 复制代码 private CookieContainer GetCookieContainer(string cookieStr)
{
CookieContainer myCookieContainer = new CookieContainer();
string[] cookstr = cookieStr.Split(';');
foreach (string str in cookstr)
{
string[] cookieNameValue = str.Split('=');
Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
ck.Domain = "xxx.xxx.xxx";
myCookieContainer.Add(ck);
}
return myCookieContainer;
}
string cookieStr = webBrowser1.Document.Cookie;
cookies.Text = cookieStr;
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
webrequest.CookieContainer = GetCookieContainer(cookieStr);
我有2個問題
(1) 如果我要把 HttpWebRequest 改成 HttpHelper,要怎麼改寫(也就是說,HttpHelper要怎麼去接 CookieContainer)
(2) 如果我要把 webBrowser1.Document.Cookie 換成站長之前說的
[C#] 纯文本查看 复制代码 //取当前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();
}
請問要怎麼改寫
麻煩了
謝謝
|