0

Im trying to get XML reposnce from External URL in MVC.

This is my Code :

        Uri uri = new Uri("http://101.10.3.111:8600/out");
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "GET";
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";

        request.ServicePoint.Expect100Continue = false;
        request.KeepAlive = false;

        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                // as an xml: deserialise into your own object or parse as you wish
                var responseXml = XDocument.Load(response.GetResponseStream());
                Console.WriteLine(responseXml.ToString());
            }
        }
        catch (Exception ex)
        {

        }

and it gives this error :

{"The server committed a protocol violation. Section=ResponseStatusLine"}

if i add this to web.config :

  <system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing="true"/>
    </settings>
  </system.net>

errors changed to this :

{"The underlying connection was closed: The connection was closed unexpectedly."}

Also if i call (copy\paste) the url into Browser it gives me output file.

Ahad Porkar
  • 1,666
  • 2
  • 33
  • 68
  • The following post suggests that you should try changing the `KeepAlive` setting if the `useUnsafeHeaderParsing` fix does not work for you: http://stackoverflow.com/questions/2482715/the-server-committed-a-protocol-violation-section-responsestatusline-error – David Tansey Oct 05 '15 at 14:14
  • i have made that change in my code : request.KeepAlive = false; – Ahad Porkar Oct 05 '15 at 14:31
  • I missed that - apologies. – David Tansey Oct 05 '15 at 14:37

0 Answers0