none
Webclient downloadstring error RRS feed

  • Question

  • Hi,

    We are using below code to download the content from xml file as string and save the string content to xml file. wc.DownloadString is failing with error as shown below:

    using (WebClient wc = new WebClient())
                {
                    wc.Headers.Add("email", "Test@somedomain.com");
                    wc.Headers.Add("password", "somepassword");
                    string xml = wc.DownloadString("https://somedomain:443/Test1/export/MyCauseAPI.xml");
                    XmlDocument downloadXML = new XmlDocument();
                    downloadXML.LoadXml(xml);
                    downloadXML.Save("C:\\Test\\New Requirements\\mctestAPI2.xml");
                }

    The underlying connection was closed: An unexpected error occurred on a send.Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

    We are not seeing this error when we access the URL https://somedomain:443/Test1/export/MyCauseAPI.xml from Postman.

    Monday, May 7, 2018 11:01 AM

All replies

  • I will suggest you to compare the headers when you send the request from Postman, and make sure you add all the missing headers.

    Also you can refer following link

    https://briancaos.wordpress.com/2012/06/15/an-existing-connection-was-forcibly-closed-by-the-remote-host/


    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    Monday, May 7, 2018 11:17 AM
  • The headers used in the above code are same as Postman

    Monday, May 7, 2018 5:04 PM
  • The "email" and "password" are sent in plain text as HTTP headers?  That violates the HTTP standard.  You aren't allowed to invent arbitrary new headers unless you use an "X-" prefix.  Are you sure they aren't sent as POST values embedded in the request data?  Does your server expect cookies?  Does it expect a User-Agent string?

    Does your server support TLS or does it require SSL?  There are a couple of web articles talking about that.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Monday, May 7, 2018 5:57 PM
  • Hi,

    The above code is part of SSIS script component and  was working without any issue from last 3 months. However, It is stopped working from 3rd May 2018. I tested this  url https://somedomain:443/Test1/export/MyCauseAPI.xml from postman. It is working. Also, I tested this from kingswaysoft adapter. It is working fine.

    Tuesday, May 8, 2018 4:19 AM
  • It's quite possible that the server changed its security attributes.  Check the article below to see if it is related to your problem:

    https://stackoverflow.com/questions/20064505/requesting-html-over-https-with-c-sharp-webclient


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Tuesday, May 8, 2018 4:56 AM