惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
V
Vulnerabilities – Threatpost
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
G
GRAHAM CLULEY
S
Securelist
P
Palo Alto Networks Blog
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
WordPress大学
WordPress大学
Project Zero
Project Zero
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
博客园 - 叶小钗
B
Blog RSS Feed
C
Cisco Blogs
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
K
Kaspersky official blog
D
Docker
Latest news
Latest news
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
Cyberwarzone
Cyberwarzone
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
C
Check Point Blog
J
Java Code Geeks
Simon Willison's Weblog
Simon Willison's Weblog
T
Tenable Blog
Recent Announcements
Recent Announcements
T
Tailwind CSS Blog
H
Help Net Security
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
Jina AI
Jina AI
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
NISL@THU
NISL@THU
美团技术团队
腾讯CDC

博客园 - Yunanw

应用SQLServer For XML 生成XML避免在C# 拼字符串 某些版本的IIS可能有SessionID混淆的Bug 小觑数据库(SqlServer)查询语句执行过程 C#中Finally的一个不太常见的用法 通过表达式树实现的ObjectMapper 实际一个多态(或弱类型)ConfigurationElementCollection 通过P/Invoke调用32位C++ DLL时,一定要将平台改为X86 坑爹的面试题 在Windows2008 IIS7上部署.net 1.1 用Python 实现刷钻网上抢任务,并实现一个简单的限制使用时间的功能 Python几行代码打印出网卡的Mac地址 放大招,解决前同事签出TFS文件的问题 飞猪传书 Linq To xml 查询时需要注意的一点。 - Yunanw 自定义WinForm 下ListBox的行高 使用LogParser分析网站运行情况(比较简单) 重新装载VS的模板 咱也搞一个TransactionScope QQ斗地主记牌器(只支持角色版) - Yunanw - 博客园
实现支持会话的WebClient
Yunanw · 2007-11-22 · via 博客园 - Yunanw

    WebClient类是一个很好工具,可以通过指定一个URL,下载文件,或上传和下载数据,在下载文件时还能实现异步下载.
但是使用WebClient访问URL时,是没有会话的,也就是说每一次使WebClient发起请求,都是一次新的会话.举一个例子.
比如:在访问页面B时要判断Session["UserName"]的值是不是空.而Session["UserName"]的值是在页面A中赋值.
如果直接使用WebClient.如下代码

WebClient client = new WebClient();
clinet.DownloadString(
@"http://PageA.aspx");
clinet.DownloadString(
@"http://PageB.aspx");

PageB中Session值也是空的.

网上有人提出了这个问题的解决办法,就是使用HttpWebRequest
如下

http://blog.joycode.com/yaodong/archive/2004/10/10/35129.aspx

CookieContainer cc = new CookieContainer();
   
for(int i=0;i<100;i++)
   
{
    HttpWebRequest myReq 
= (HttpWebRequest)WebRequest.Create("http://localhost/AspxApp/MainForm.aspx");
    myReq.CookieContainer 
= cc;
    HttpWebResponse resp 
= myReq.GetResponse() as HttpWebResponse;
    Stream s 
= resp.GetResponseStream();
    StreamReader sr 
= new StreamReader(s);    String text = sr.ReadToEnd();
    sr.Close();
    s.Close();
   }


这样当然可以解决问题,但是有一个不好地方,就是HttpWebRequest的功能比较弱,只能返回流,下载文件时不能异步,没有进度.
    我通过使用Reflector查看了一下WebClient的代码,发现WebClient每次发请求(如DownloadString)时都会调用自己的一个叫GetWebRequest的方法,查MSDN,发现GetWebRequest 方法签名如下

protected virtual WebRequest GetWebRequest (
    Uri address
)

这也就意味着,如果我们重写这个方法,把CookeContainer给这个WebRequest加上,那么WebClinet就支持会话了.
实现代码如下:

 public class HttpWebClient:WebClient
    
{
        
private CookieContainer cookie ;
       
        
protected override WebRequest GetWebRequest(Uri address)
        
{
            
//throw new Exception();
            WebRequest request ;

            request 
= base.GetWebRequest(address);
            
//判断是不是HttpWebRequest.只有HttpWebRequest才有此属性
            if (request is HttpWebRequest)
            
{
                HttpWebRequest httpRequest 
= request as HttpWebRequest;

                httpRequest.CookieContainer 
= cookie;
            }


            
return request;
        }


        
public HttpWebClient(CookieContainer cookie)
        
{
            
this.cookie = cookie;
        }

    }

调用代码如下:

ttpWebClient httpClient = new HttpWebClient(new CookieContainer());

            String s 
= httpClient.DownloadString(@"http://localhost/TestWS/Default.aspx");
            s 
= httpClient.DownloadString(String.Format(@"http://localhost/TestWS/Test.aspx?Test={0}",s));

                      

            Console.WriteLine(s);

这篇Blog是我在没开VS2005的情况下写的,可能代码会有小的问题编译不能通过之类的,应该无大的问题!希望对大家有用