当你设置HttpWebRequest.Proxy = null时,我发现了HttpWebRequest类的疯狂行为。由于某种原因,它首次将Expect100Continue重新设置为true。例如这段代码
HttpWebRequest webRequest1 = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com/");
webRequest1.Method = "POST";
webRequest1.ServicePoint.Expect100Continue = false;
webRequest1.Proxy = GlobalProxySelection.GetEmptyWebProxy(); ;
Console.WriteLine(webRequest1.ServicePoint.Expect100Continue);
webRequest1.ServicePoint.Expect100Continue = false;
webRequest1.Proxy = null;
Console.WriteLine(webRequest1.ServicePoint.Expect100Continue);
webRequest1 = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com/");
webRequest1.Method = "POST";
webRequest1.ServicePoint.Expect100Continue = false;
webRequest1.Proxy = null;
Console.WriteLine(webRequest1.ServicePoint.Expect100Continue);
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com/");
webRequest2.Method = "POST";
webRequest2.ServicePoint.Expect100Continue = false;
webRequest2.Proxy = null;
Console.WriteLine(webRequest2.ServicePoint.Expect100Continue);
Console.ReadLine();
会写: 真正 假 假 假
我发现只有一种方法可以解决这个问题:
var Expect100Continue = webRequest.ServicePoint.Expect100Continue;
webRequest.Proxy = null;
webRequest.ServicePoint.Expect100Continue = Expect100Continue;
任何人都能解释一下吗?
答案 0 :(得分:1)
在Proxy
上设置WebRequest
属性时,将重新创建ServicePoint
。
Expect100Continue
的值将默认为可以从app.config文件配置的ServicePointManager
上设置的值。
至少,这是我从反汇编中看到的。