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

推荐订阅源

GbyAI
GbyAI
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
D
Docker
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
美团技术团队
V
V2EX
Last Week in AI
Last Week in AI
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Microsoft Security Blog
Microsoft Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
B
Blog RSS Feed
博客园_首页
B
Blog
博客园 - 叶小钗
I
InfoQ
WordPress大学
WordPress大学
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
The Register - Security
The Register - Security
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Latest news
Latest news
W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News and Events Feed by Topic
S
Secure Thoughts
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News

博客园 - 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的代理设置;