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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - RobotTech

[Z] 从Uncaught SyntaxError: Unexpected token ")" 问题看javascript:void的作用 [Z] SQL SERVER 的前世今生--各版本功能对比 关于.NET编译的目标平台(AnyCPU,x86,x64) (转) [转] HTTP Headers 入门 [转] 一个小时学会Git 用命令编译 js事件之event.preventDefault()与event.stopPropagation()用法区别 [转] The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing [转] Visual Studio Code behind a proxy [转] js == 与 === 的区别 [转]说说C#的async和await Oracle 12c SQL Server死锁 Initialize the Storage Emulator by Using the Command-Line Tool Microsoft Fakes Identifier 'Logic.DomainObjectBase._isNew' is not CLS-compliant SSD TRIM Visual Studio 2013 prerequisites 怎样用UltraISO制作U盘系统安装盘
[Z] C#程序中设置全局代理(Global Proxy)
RobotTech · 2018-06-23 · via 博客园 - RobotTech

https://www.cnblogs.com/Javi/p/7274268.html

1. HttpWebRequest类的Proxy属性,只要设置了该属性就能够使用代理了,如下:

1             //设置代理

2         WebProxy WP = new WebProxy("41.76.44.76", 3128);

3             ICredentials jxCredt = new NetworkCredential("proxy_username", "proxy_password");

4             WP.Credentials = jxCredt;

5            

6             HttpWebRequest webreq = HttpWebRequest.Create(uri);

7             webreq.Proxy = WP;//将代理赋值给HttpWebRequest的Proxy属性   

2.但是程序中的每一个HttpWebRequest都需要如此设置,是否存在更简便的方法,例如在某个地方设置了代理,则整个程序的所有请求都使用代理呢。

   答案是:有这样的方法。

   ===================================

   不过在此之前先来理解一下 C#的WebRequest类(即HtteWebRequest的父类)在发送前的一些处理:

          新创建一个WebRequest实例时(通过Create方法),会自动初始化其Proxy属性,

    而它还有一个DefaultWebProxy属性,当用户没有手动设置Proxy属性时,

    则WebRequest会使用DefaultWebProxy作为其Proxy;

    而DefaultWebProxy是读取项目的app.config文件来进行初始化;

    当没有app.config文件,或者没有在app.config中配置Proxy时,

    DefaultWebProxy就会去读取Internet Explorer (IE)的代理设置

   ============================================

    提示:

          怎么查看当前的请求Rquest是否使用了代理?

3.所以设置全局代理的方式有以下几种:

   第一种:程序不做任何处理,仅设置IE的代理;

   第二种:在程序的app.config文件进行相关的配置,官方文档,如:

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <configuration>

 3   <system.net>

 4     <defaultProxy>

 5       <proxy

 6         proxyaddress="http://192.168.1.10:3128"

 7       />

 8     </defaultProxy>

 9   </system.net>

10 </configuration>

            使用app.config设置代理时,如果想在运行时修改代理,则需要编辑app.config文件,

            可以参考:C#读写config配置文件

 第三种:在代码中为DefaultWebProxy重新赋值,如下:         

1 WebProxy WP = new WebProxy("41.76.44.76", 3128);

2 ICredentials jxCredt = new NetworkCredential("proxy_username", "proxy_password");//如果有用户名和密码需要设置

3 WP.Credentials = jxCredt;

4

5 WebRequest.DefaultWebProxy = WP;

4.总结:

    第一种直接在IE上设置的,更多适合平时测试,因为让用户去作这种设置很不理智;

    第二种和第三种都相对简单,只要设置好,整个程序的所有WebRequest都会使用代理,但第二种还需要对app.config文件进行编辑,所以非硬性要求,建议使用第三种,设置DefaultWebProxy的方式;

    假如不想使用代理,则将DefaultWebProxy设为null,这样即使IE或者app.config设置了代理也不会影响。

    代理的优先顺序:手动设置WebRequest.Proxy属性 > DefaultWebProxy > app.config > IE的代理设置;