Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArgumentException when setting httpWebRequest.Connection to "Closed"

I am trying to connect to a php apache 2.2.1 webservice like this:

        protected override WebRequest GetWebRequest(Uri uri)
        {
            var req = base.GetWebRequest(uri);
            var http = req as HttpWebRequest;
            if (http != null)
            {
                http.Connection = "Close";
                http.ServicePoint.Expect100Continue = false;
            }
            return req;
        }

But I received an System.ArgumentException mean as following "Keep-Alive and Close cannot be set"(This message has been translated from Japanese).

I have no idea why this exception is thrown. I have searched in MSDN and there is an explanation like this Exception Condition: The value of Connection is set to Keep-alive or Close.. In this case my httpWebRequest.Connection is null.

Do you have any idea about the reason of this exception?

like image 618
Man Perfect Avatar asked Mar 18 '26 08:03

Man Perfect


1 Answers

I cannot explain why setting HttpWebRequest.Connection to Close or Keep-alive throw an ArgumentException. But we can set the Connection header to Close via the setting HttpWebRequest.KeepAlive to false.

More infos here : https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.connection?view=netframework-4.7.2

like image 174
Man Perfect Avatar answered Mar 20 '26 23:03

Man Perfect



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!