积极答复者
使用HttpWebRequest登录后不能获得内容

问题
-
我使用webBrowser显示网页登录页面,填入登录用户名,密码后登录,然后取得
webBrowser1.Document.Cookie存入CookieContainer
再使用HttpWebRequest来post,可以取得相关内容。
但是我使用HttpWebRequest来POST用户名、密码然后获得HttpWebResponse.Cookies,再存入CookieContainer,使用这个CookieContainer。就不能获得内容。
对比了两个Cookie其中的属性完全一致。
请问这是因为我没有通过网页的验证吗?还是别的可能?
THX
方法一:
CookieContainer myContainer = new CookieContainer();
myContainer = cookieservice.GetCookie(webBrowser1.Document.Cookie);
CookieContainer GetCookie(string webBrowserCookie)
{
CookieContainer container = new CookieContainer();
foreach (string cookie in webBrowserCookie.Split(';'))
{
string name = cookie.Split('=')[0];
string value = cookie.Substring(name.Length + 1);
string path = "/";
string domain = "XXXX";
container.Add(new Cookie(name.Trim(), value.Trim(), path, domain));
}
return container;
}string text = DocIdService.GetDocID(myContainer,"select * from table");
方法二:
CookieContainer myContainer = new CookieContainer();
myContainer = CookieService.GetCookieContainer();CookieContainer GetCookieContainer()
{
string postdata = "UserCode=username&PWD=password&StationCode=86";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postdata);
HttpWebRequest myRequest=(HttpWebRequest)WebRequest.Create("http://LogonSubmit.jsp");
myRequest.Method = "post";
myRequest.Accept = "*/*";
myRequest.CookieContainer = new CookieContainer();
Stream myStream = myRequest.GetRequestStream();
myStream.Write(data, 0, data.Length);
myStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
CookieContainer container = new CookieContainer();
foreach (Cookie cookie in myResponse.Cookies)
{
string name = cookie.Name;
string value = cookie.Value;
string path = cookie.Path;
string domain = cookie.Domain;
container.Add(new Cookie(name.Trim(), value.Trim(), path, domain));
}
return container;
}string text = DocIdService.GetDocID(myContainer,"select * from table");
- 已编辑 smith hans 2013年3月12日 6:50
答案
-
用webbrowser登录后,可以用以下代码获得
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrl, string lpszCookieName, string lpszCookieData);
public static string GetCookieString(string url)
{
// Determine the size of the cookie
uint datasize = 256;
StringBuilder cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))
{
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))
return null;
}
return cookieData.ToString();
}CookieContainer GetCookieContainer(string url)
{
CookieContainer myContainer = new CookieContainer();
string cookieStr = GetCookieString(url);
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 = Properties.Settings.Default.storeDomain;
myContainer .Add(ck);
}return myContainer;
}
- 已编辑 halftone1860 2013年4月19日 8:22
- 已标记为答案 Mike FengModerator 2013年5月4日 14:28
全部回复
-
方便的话请给出源代码,谢谢!
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
public string GetDocID(CookieContainer myContainer, string sqlString)
{
string postdata = sqlString;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postdata);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://EasyQueryXML.jsp");
myRequest.Method = "post";
myRequest.CookieContainer = myContainer;
Stream myStream = myRequest.GetRequestStream();
myStream.Write(data, 0, data.Length);
myStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
string content = reader.ReadToEnd();
return content;
} -
Hi Smith,
除了cookie,应该还和session相关吧。
Mike Feng
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
用webbrowser登录后,可以用以下代码获得
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrl, string lpszCookieName, string lpszCookieData);
public static string GetCookieString(string url)
{
// Determine the size of the cookie
uint datasize = 256;
StringBuilder cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))
{
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))
return null;
}
return cookieData.ToString();
}CookieContainer GetCookieContainer(string url)
{
CookieContainer myContainer = new CookieContainer();
string cookieStr = GetCookieString(url);
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 = Properties.Settings.Default.storeDomain;
myContainer .Add(ck);
}return myContainer;
}
- 已编辑 halftone1860 2013年4月19日 8:22
- 已标记为答案 Mike FengModerator 2013年5月4日 14:28