关于C# httpwebrequest Cookie 的问题
左边是访问页面监测到的有cookie右边是我用httpwebrequest访问的没有cookie下面是代码求赐教stringurl="http://webgame.ali...
左边是访问页面监测到的 有cookie 右边是我用httpwebrequest访问的 没有cookie
下面是代码 求赐教
string url = "http://webgame.ali213.net/webgame_action/code.php"; //验¨¦证¡è码?页°3面?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "*/*";
request.Method = "GET";
request.Referer
= "http://web.ali213.net/fahao/1260994.html";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.5; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; doyo 2.6.1)";
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.Headers.Add("Accept-Language", "zh-CN");
request.CookieContainer = cookies;
request.CookieContainer = new CookieContainer(); //暂Y存ä?到Ì?新?实º¦Ì例¤y
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
cookies = request.CookieContainer; //保À¡ê存ä?cookies
string strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);
在线等,好像没分了 展开
下面是代码 求赐教
string url = "http://webgame.ali213.net/webgame_action/code.php"; //验¨¦证¡è码?页°3面?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "*/*";
request.Method = "GET";
request.Referer
= "http://web.ali213.net/fahao/1260994.html";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.5; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; doyo 2.6.1)";
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.Headers.Add("Accept-Language", "zh-CN");
request.CookieContainer = cookies;
request.CookieContainer = new CookieContainer(); //暂Y存ä?到Ì?新?实º¦Ì例¤y
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
cookies = request.CookieContainer; //保À¡ê存ä?cookies
string strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);
在线等,好像没分了 展开
1个回答
展开全部
获取完整cookie代码
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
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)]
static extern int InternetSetCookieEx(string lpszURL, string lpszCookieName, string lpszCookieData, int dwFlags, IntPtr
dwReserved);
/// <summary>
/// 获取完整cookie
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static string GetCookies(string url)
{
uint datasize = 256;
StringBuilder cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero))
{
if (datasize < 0)
return null;
cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))
return null;
}
return cookieData.ToString();
}
追答
对啊
例如模拟登录时候,你登录以后就要拿到cookie、然后存起来,request的时候,在赋值给request.CookieContainer
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询