locked
Failed to connect to remote server with WebClient.DownloadString () method RRS feed

  • Question

  • User-1184006624 posted

    Hi, I use the WebClient to read web pages from my asp.net application. For that, I used  WebClient with webClient.DownloadString method. I get the following exection each time I try to connect:

    InnerException = {"A connection attempt failed because the connected party did not respond properly after a certain amount of time or an established connection failed because the login host did not respond 160.119.189.95:80"};  Message = "Can not connect to the remote server"

    Is there a solution to this problem?

    Code example:

     WebClient client = new WebClient();
     try{
      
        client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
    
        string s = client.DownloadString("https://en.wikipedia.org/wiki/Malaria");
        //other things here
    }catch (Exception e)
    {}

    Sunday, February 17, 2019 8:39 AM

All replies

  • User-943250815 posted

    Your code is fine and working. A timeout can happen.

    But there is something not clear on your provided info.
    Exception say 160.119.189.95:80, this is an HTTP not HTTPS, at this place there is an index.html with Bad file descriptor
    Your code shows https://en.wikipedia.org, which not resolves to 160.119.189.95

    Sunday, February 17, 2019 11:33 AM
  • User-893317190 posted

    Hi bokokibiti,

    I have tested your code in my computer but I could get response successfully.

    Are you sure your computer could connect to the server?

    Have you tried to directly paste the address in browser to see whether you could get the response?

    If the network between your computer and your remoter server is ok, this may have something to do with proxy setting of your web.config,you could disable it.

    <configuration>
      <system.net>
        <defaultProxy enabled="false"></defaultProxy>
      </system.net>

    Best regards,

    Ackerly Xu

    Monday, February 18, 2019 3:22 AM
  • User-1184006624 posted

    Hi jzero. 

    I do not understand when you say : "at this place there is an index.html with Bad file descriptor
    Your code shows https://en.wikipedia.org, which not resolves to 160.119.189.95".

    what does it mean? Because I do not always work. While the link is easily accessible on the browser

    Monday, February 18, 2019 11:32 AM
  • User-1184006624 posted

    Thank you for your answer Ackerly Xu. 

    Are you sure your computer could connect to the server?  Yes

    I add the code in the web.config. it does not always work

    <system.net>
        <defaultProxy enabled="false"></defaultProxy>
      </system.net>

    Monday, February 18, 2019 11:45 AM
  • User-943250815 posted

    @Ackerly Xu, you right, but only if there is a way to bypass network proxy

    @bokokibiti
    Pay attention to InnerMessage

    bokokibiti

    InnerException = {"A connection attempt failed because the connected party did not respond properly after a certain amount of time or an established connection failed because the login host did not respond 160.119.189.95:80"}; Message = "Can not connect to the remote server"


    Is 160.119.189.95 a proxy server with authentication?
    To me seems it is. In this case you have to set Proxy data on webclient before call url

    Try
    Dim myProxyIP as string = "use ip address of your proxy"
    Dim myProxyPort as integer = "use port of your proxy"
    Dim myUser as string = "user name here"
    Dim myPwd as string = "user password here" Dim myProxy As System.Net.WebProxy = New System.Net.WebProxy(myProxyIP, myProxyPort) With {.Credentials = New System.Net.NetworkCredential(myUser, myPwd)} Dim myClient As New System.Net.WebClient myClient.Proxy = myProxy myClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") Dim myString As String = myClient.DownloadString("https://en.wikipedia.org/wiki/Malaria") Catch ex As Exception End Try

    Monday, February 18, 2019 1:14 PM
  • User-1184006624 posted

    OK I try to follow your method

    Monday, February 18, 2019 1:36 PM