Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the http version of my web method call to stop 505 error?

I need to get some data from a service provider and have configured our .net app to point to their hosted web service to get the data. Using the code below, when the web method is called (ws.DoTransfer) I get the following error...

    private void DoTransferLocal()
    {
            Version version = new Version();
            string error = string.Empty;
            try
            {
                    RemoteService ws = new RemoteService();
                    ServicePoint spm = ServicePointManager.FindServicePoint(new Uri(ws.Url));
                    spm.Expect100Continue = true;
                    version = spm.ProtocolVersion;
                    ws.Credentials = credentials;
                    ws.PreAuthenticate = true;
                    RemoteResult result = ws.DoTransfer();
                    MessageBox.Show("Result = " + result.transferStatus);
            }
            catch (Exception ex)
            {
                    error = ex.Message;
            }
            finally
            {
                    MessageBox.Show(version.ToString() + Environment.NewLine + error);
            }
    }

Error:

The request failed with HTTP status 505: HTTP Version Not Supported.

I've been told that the version of the HTTP needs to be 1.0, but mine is 1.1

I've read a couple of google posts about this and have seen suggestions to override the GetWebRequest method as shown here...

    protected override System.Net.WebRequest GetWebRequest(Uri uri)
    {   
            System.Net.HttpWebRequest request = base.GetWebRequest(uri) as System.Net.HttpWebRequest;
            request.ProtocolVersion = System.Net.HttpVersion.Version10;
            return request;
    }

...but when I try this, the GetWebRequest after base. is underlined in red and has the error...

'Object' does not contain a definition for 'GetWebRequest'

Can anyone tell me how I change the HTTP version to 1.0, but still use similar code (rather than building up my own soap packets) to call my web method?

I can't seem to find any kind of code that I can simply inject into my code that looks like the following line...

    ws.HttpVersion = HttpVersion.Version10;

Thanks

like image 720
Karl Avatar asked Jan 06 '12 20:01

Karl


2 Answers

Change the Expect100Continue to false. You can do it in the config file of you app, adding the following:

<configuration>
    <system.net>
        <settings>
            <servicePointManager expect100Continue="false" />
        </settings>
    </system.net>
</configuration>
like image 60
Catalin S Avatar answered Nov 03 '22 01:11

Catalin S


If you are unable to override GetWebRequest, you are using a WCF service and not a Soap web service. WCF does not support HTTP/1.0

To create a Web Service reference that will allow you to use the HTTP/1.0 protocol

  • Right click on project and select Add Service.
  • Click on the Advanced button
  • Click on the Add Web Reference

If you use the same namespace as before, no code changes should be required

like image 32
Chip Willman Avatar answered Nov 03 '22 01:11

Chip Willman