The underlying connection was closed

"The underlying connection was closed: The server committed an HTTP protocol violation."

This is error that im getting when i execute the code shown below..

Uri ourUri = new Uri("http://webservices.lb.lt/ExchangeRates/ExchangeRates.asmx ");

// Creates an HttpWebRequest for the specified URL.

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(ourUri);

myHttpWebRequest.ProtocolVersion = HttpVersion.Version10;

myHttpWebRequest.Timeout = 100000;

// Sends the HttpWebRequest and waits for the response.

HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

// Ensures that only Http/1.0 responses are accepted.

if(myHttpWebResponse.ProtocolVersion != HttpVersion.Version10)

MessageBox.Show("\nThe server responded with a version other than Http/1.0");

else

if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)

Console.WriteLine("\nRequest sent using version Http/1.0. Successfully received response with version HTTP/1.0 ");

// Releases the resources of the response.

myHttpWebResponse.Close();

I tried setting the protocol version to HttpVersion.Version11; , didnt work out though.

Please help..




Answer this question

The underlying connection was closed

  • waGordon

    The following code works for me on both .NET Framework 1.1 and
    .NET Framework 2.0. versions.

    What exactly is the framwork version you are using


    In any case if you still encounter the Protocol violation, you might want to use
    this following config file
    < xml version="1.0" encoding="UTF-8" >
    <configuration>
     <system.net>
                <settings>
                      <httpWebRequest useUnsafeHeaderParsing="true"/>
                </settings>
     </system.net>
    </configuration>




    using System;

    using System.Net;

    using System.Text;

    using System.IO;

    using System.Windows.Forms;

     

    public class Test

    {

    public static void Main()

    {

    Uri ourUri = new Uri("http://webservices.lb.lt/ExchangeRates/ExchangeRates.asmx ");

    // Creates an HttpWebRequest for the specified URL.

    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(ourUri);

    myHttpWebRequest.ProtocolVersion = HttpVersion.Version10;

    myHttpWebRequest.Timeout = 100000;

    // Sends the HttpWebRequest and waits for the response.

    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

    // Ensures that only Http/1.0 responses are accepted.

    if (myHttpWebResponse.ProtocolVersion != HttpVersion.Version10)

    MessageBox.Show("\nThe server responded with a version other than Http/1.0");

    else

    if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)

    Console.WriteLine("\nRequest sent using version Http/1.0. Successfully received response with version HTTP/1.0 ");

    // Releases the resources of the response.

    myHttpWebResponse.Close();

     

    /*

    HttpWebRequest hrq = (HttpWebRequest)WebRequest.Create("http://webservices.lb.lt/ExchangeRates/ExchangeRates.asmx");

    HttpWebResponse hrs = (HttpWebResponse) hrq.GetResponse();

    Stream s = hrs.GetResponseStream();

    StreamReader sr = new StreamReader(s, Encoding.UTF8);

    String html = sr.ReadToEnd();

    Console.WriteLine(html);

    s.Close();

    sr.Close();

    hrs.Close();

    */

    }

    }



     


  • antwan15

    Thanks durga, it works fine now...

    Im using framework 1.1 with SP1 installed...

  • The underlying connection was closed