小隐的博客

人生在世,笑饮一生
随笔 - 304, 文章 - 0, 评论 - 349, 阅读 - 50万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

用httpwebrequest访问跨域网站时对CookieContainer的处理

Posted on   隐客  阅读(3456)  评论(3编辑  收藏  举报

前几天,想对一个网站实现自动登陆后提交相关数据,没有想到一直登陆不成功(验证码输入正确),后来用我的 WebClient 制作调试Http的post 和 get 工具 调试又可以的,郁闷!

 

第二天早上起来,突然想到可能是跨域的问题,之前有见过说CookieContainer在提交时,只发送当前域的cookie,其它的不提交,在网上搜索了一番,果然找到了 见 http://blog.csdn.net/ETstudio/archive/2007/11/21/1897071.aspx,我也把相关Dll Reflector 了一遍,果然找到了

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
QQ交流
点击右上角即可分享
微信分享提示