用httpwebrequest访问跨域网站时对CookieContainer的处理
2010-09-23 10:25
561 查看
前几天,想对一个网站实现自动登陆后提交相关数据,没有想到一直登陆不成功(验证码输入正确),后来用我的 WebClient 制作调试Http的post 和 get 工具 调试又可以的,郁闷!
第二天早上起来,突然想到可能是跨域的问题,之前有见过说CookieContainer在提交时,只发送当前域的cookie,其它的不提交,在网上搜索了一番,果然找到了 见 http://blog.csdn.net/ETstudio/archive/2007/11/21/1897071.aspx,我也把相关Dll Reflector 了一遍,果然找到了
由此可见,问题也就找到了,于是自己写了一个方法来处理cookie,不过,也要注意,接收的时候,cookie的存放是有区别的!!!
另外,顺便贴一下,页面前台是如何解决跨域问题的
见疯狂的跨域技术:http://itgeeker.com/mathml/readpaper?pid=53
第二天早上起来,突然想到可能是跨域的问题,之前有见过说CookieContainer在提交时,只发送当前域的cookie,其它的不提交,在网上搜索了一番,果然找到了 见 http://blog.csdn.net/ETstudio/archive/2007/11/21/1897071.aspx,我也把相关Dll Reflector 了一遍,果然找到了
namespace System.Net { using System; internal static class CookieModule { internal static void OnReceivedHeaders(HttpWebRequest httpWebRequest) { try { if (httpWebRequest.CookieContainer != null) { HttpWebResponse response = httpWebRequest._HttpResponse; if (response != null) { CookieCollection cookies = null; try { string setCookie = response.Headers.SetCookie; if ((setCookie != null) && (setCookie.Length > 0)) { cookies = httpWebRequest.CookieContainer.CookieCutter(response.ResponseUri, "Set-Cookie", setCookie, false); } } catch { } try { string setCookieHeader = response.Headers.SetCookie2; if ((setCookieHeader != null) && (setCookieHeader.Length > 0)) { CookieCollection cookies2 = httpWebRequest.CookieContainer.CookieCutter(response.ResponseUri, "Set-Cookie2", setCookieHeader, false); if ((cookies != null) && (cookies.Count != 0)) { cookies.Add(cookies2); } else { cookies = cookies2; } } } catch { } if (cookies != null) { response.Cookies = cookies; } } } } catch { } } internal static void OnSendingHeaders(HttpWebRequest httpWebRequest) { try { if (httpWebRequest.CookieContainer != null) { string str; httpWebRequest.Headers.RemoveInternal("Cookie"); string cookieHeader = httpWebRequest.CookieContainer.GetCookieHeader(httpWebRequest.Address, out str); if (cookieHeader.Length > 0) { httpWebRequest.Headers["Cookie"] = cookieHeader; } } } catch { } } } }
由此可见,问题也就找到了,于是自己写了一个方法来处理cookie,不过,也要注意,接收的时候,cookie的存放是有区别的!!!
另外,顺便贴一下,页面前台是如何解决跨域问题的
见疯狂的跨域技术:http://itgeeker.com/mathml/readpaper?pid=53
相关文章推荐
- 转 跨域读取Cookie和session之HttpWebRequest另类方法(网站API开发)
- C#利用HttpWebRequest 测试网站是否可以正常访问
- VB.net使用httpWebRequest访问带cookie的网站时如何传递cookie值
- C# httpwebrequest访问HTTPS错误处理
- 跨域读取Cookie和session之HttpWebRequest另类方法(网站API开发)
- HttpWebRequest跨域cookie的访问
- 使用C#的HttpWebRequest访问网站
- 使用HttpWebRequest访问需要登录的网站-另一种途径
- 2008年3月小记(设置Windows服务的依赖关系,配置MSMQ的访问权限,WCF的安全配置,删除重建网站,HttpWebRequest,一年中的周,在WCF中使用HttpContext,String.Join)
- 跨域读取Cookie和session之HttpWebRequest另类方法(网站API开发)
- C# httpwebrequest访问HTTPS错误处理方法
- 2008年3月小记(设置Windows服务的依赖关系,配置MSMQ的访问权限,WCF的安全配置,删除重建网站,HttpWebRequest,一年中的周,在WCF中使用HttpContext,String.Join)
- 跨域读取Cookie和session之HttpWebRequest另类方法(网站API开发)
- 使用C#的HttpWebRequest模拟登陆网站
- 使用 HttpWebRequest 向网站提交数据
- 使用 HttpWebRequest 向网站提交数据
- 使用C#的HttpWebRequest模拟登陆网站
- 使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- ajax调用handler,使用HttpWebRequest访问WCF服务