HttpWebRequest.Proxy = null重写Expect100Continue

时间:2014-03-24 11:09:33

标签: .net httpwebrequest

当你设置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;

任何人都能解释一下吗?

1 个答案:

答案 0 :(得分:1)

Proxy上设置WebRequest属性时,将重新创建ServicePoint

Expect100Continue的值将默认为可以从app.config文件配置的ServicePointManager上设置的值。

至少,这是我从反汇编中看到的。