WebResponse.GetResponse Error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan

    #1

    WebResponse.GetResponse Error

    Calling the System.Net.WebR esponse.GetResp onse method (see
    code below) results in the following error:

    System.Net.WebE xception: The underlying connection was
    closed: An unexpected error occurred on a receive.
    at(System.Net.H ttpWebRequest.C heckFinalStatus ())
    at System.Net.Http WebRequest.EndG etResponse(IAsy ncResult
    asyncResult)
    at(System.Net.H ttpWebRequest.G etResponse())

    The URL works ok in IE on the same PC.

    Does anyone know what would cause this problem, what other
    checks I could try or any other method to do the same
    thing as System.Net.WebR esponse.GetResp onse?

    Thank you in advance.

    Private Function HasWebConnectio n() As Boolean
    Dim bResult As Boolean
    Try
    bResult = True
    Dim URI As New System.Uri("htt p://www.microsoft.c om")
    Dim TestRequest As Net.WebRequest =
    Net.WebRequest. Create(URI)
    TestRequest.Tim eout = Threading.Timeo ut.Infinite
    Dim TestResponse As Net.WebResponse =
    TestRequest.Get Response
    Catch ex As Exception
    Console.WriteLi ne(ex.ToString)
    bResult = False
    End Try
    Return bResult
    End Function

  • Tian Min Huang

    #2
    RE: WebResponse.Get Response Error

    Hi,

    Thanks for your post. I tested your code, and it works properly on my side.
    Now I'd like to share the following information with you:

    1. Since IE works properly, I'd like you to check connection setting in IE
    options, especially, the proxy setting. You can use WebProxy to set HTTP
    proxy for the WebRequest class.

    WebProxy Class
    http://msdn.microsoft.com/library/de...us/cpref/html/
    frlrfsystemnetw ebproxyclasstop ic.asp

    2. I recommend you use the sample in KB article 303436 and check whether or
    not it works on your side.
    SAMPLE: Visual C# .NET Networking Classes HTTP Internet Client
    Microsoft Support is here to help you with Microsoft products. Find how-to articles, videos, and training for Microsoft Copilot, Microsoft 365, Windows 11, Surface, and more.


    I look forward to your result.

    Have a nice day!

    Regards,

    HuangTM
    Microsoft Online Partner Support
    MCSE/MCSD

    Get Secure! -- www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.

    Comment

    • Guest's Avatar

      #3
      RE: WebResponse.Get Response Error

      Hi,

      Thank you for your informative reply.

      When I checked the proxy settings in IE I found that under
      the dial-up connection settings the proxy was disabled but
      under LAN Settings that the proxy was incorrectly enabled.
      Since I am using a dial-up connection I did not think that
      this would make any difference (It even states "These
      settings will not apply to any dial-up or VPN
      connections.") but as a test, I disabled them anyway and
      much to my surprise the code worked. I also tried
      disconnecting the network cable but it made no difference.
      The code always attempted to use the proxy settings under
      LAN Settings regardless of the fact that the connection to
      the Internet was via a dial-up connection.

      This raises another question. Many of my application users
      will have laptops that they will connect to a LAN when
      they are in the office and to the Internet via dial-up
      connections at home. I was hoping just to use the settings
      in IE and not have the user set up special settings in my
      application. Is there a way of checking which are the
      appropriate proxy settings to use.

      Thank you,

      Jonathan
      [color=blue]
      >-----Original Message-----
      >Hi,
      >
      >Thanks for your post. I tested your code, and it works[/color]
      properly on my side.[color=blue]
      >Now I'd like to share the following information with you:
      >
      >1. Since IE works properly, I'd like you to check[/color]
      connection setting in IE[color=blue]
      >options, especially, the proxy setting. You can use[/color]
      WebProxy to set HTTP[color=blue]
      >proxy for the WebRequest class.
      >
      >WebProxy Class
      >http://msdn.microsoft.com/library/default.asp?[/color]
      url=/library/en-us/cpref/html/[color=blue]
      >frlrfsystemnet webproxyclassto pic.asp
      >
      >2. I recommend you use the sample in KB article 303436[/color]
      and check whether or[color=blue]
      >not it works on your side.
      >SAMPLE: Visual C# .NET Networking Classes HTTP Internet[/color]
      Client[color=blue]
      >http://support.microsoft.com/?id=303436
      >
      >I look forward to your result.
      >
      >Have a nice day!
      >
      >Regards,
      >
      >HuangTM
      >Microsoft Online Partner Support
      >MCSE/MCSD
      >
      >Get Secure! -- www.microsoft.com/security
      >This posting is provided "as is" with no warranties and[/color]
      confers no rights.[color=blue]
      >
      >.
      >[/color]

      Comment

      • Jonathan

        #4
        RE: WebResponse.Get Response Error

        Hi,

        Sorry for the double post. I forgot provide my alias.

        Thank you for your informative reply.

        When I checked the proxy settings in IE I found that under
        the dial-up connection settings the proxy was disabled but
        under LAN Settings that the proxy was incorrectly enabled.
        Since I am using a dial-up connection I did not think that
        this would make any difference (It even states "These
        settings will not apply to any dial-up or VPN
        connections.") but as a test I disabled them anyway and
        much to my surprise the code worked. I also tried
        disconnecting the network cable but it made no difference.
        The code always attempted to use the proxy settings under
        LAN Settings regardless of the fact that the connection to
        the Internet was via a dial-up connection.

        This raises another question. Many of my application users
        will have laptops that they will connect to a LAN when
        they are in the office and to the Internet via dial-up
        connections at home. I was hoping just to use the settings
        in IE and not have the user set up special settings in my
        application. Is there a way of checking which are the
        appropriate proxy settings to use.

        Thank you,

        Jonathan
        [color=blue]
        >-----Original Message-----
        >Hi,
        >
        >Thanks for your post. I tested your code, and it works[/color]
        properly on my side.[color=blue]
        >Now I'd like to share the following information with you:
        >
        >1. Since IE works properly, I'd like you to check[/color]
        connection setting in IE[color=blue]
        >options, especially, the proxy setting. You can use[/color]
        WebProxy to set HTTP[color=blue]
        >proxy for the WebRequest class.
        >
        >WebProxy Class
        >http://msdn.microsoft.com/library/default.asp?[/color]
        url=/library/en-us/cpref/html/[color=blue]
        >frlrfsystemnet webproxyclassto pic.asp
        >
        >2. I recommend you use the sample in KB article 303436[/color]
        and check whether or[color=blue]
        >not it works on your side.
        >SAMPLE: Visual C# .NET Networking Classes HTTP Internet[/color]
        Client[color=blue]
        >http://support.microsoft.com/?id=303436
        >
        >I look forward to your result.
        >
        >Have a nice day!
        >
        >Regards,
        >
        >HuangTM
        >Microsoft Online Partner Support
        >MCSE/MCSD
        >
        >Get Secure! -- www.microsoft.com/security
        >This posting is provided "as is" with no warranties and[/color]
        confers no rights.[color=blue]
        >
        >.
        >[/color]

        Comment

        • Tian Min Huang

          #5
          RE: WebResponse.Get Response Error

          Hi Jonathan,

          Thanks for your update. Generally speaking, the WebRequest will get the
          proxy information from the settings in IE. If it is using DUN, you can add
          the following code to clear the proxy:

          Dim myProxy = New Net.WebProxy
          TestRequest.Pro xy = myProxy

          Hope this helps.

          Regards,

          HuangTM
          Microsoft Online Partner Support
          MCSE/MCSD

          Get Secure! -- www.microsoft.com/security
          This posting is provided "as is" with no warranties and confers no rights.

          Comment

          • Jonathan

            #6
            RE: WebResponse.Get Response Error

            Hi,

            Isn't this a bug in WebRequest? It is not getting using
            the proxy settings of the active connection from IE. It
            uses the settings under LAN even when there is no LAN
            connection.

            I see that the code that you sent would clear the proxy
            but how would I detect that the user is has a DUN
            connection and then how do I know if they don't have proxy
            settings on the DUN.

            Thanks,

            Jonathan
            [color=blue]
            >-----Original Message-----
            >Hi Jonathan,
            >
            >Thanks for your update. Generally speaking, the[/color]
            WebRequest will get the[color=blue]
            >proxy information from the settings in IE. If it is using[/color]
            DUN, you can add[color=blue]
            >the following code to clear the proxy:
            >
            > Dim myProxy = New Net.WebProxy
            > TestRequest.Pro xy = myProxy
            >
            >Hope this helps.
            >
            >Regards,
            >
            >HuangTM
            >Microsoft Online Partner Support
            >MCSE/MCSD
            >
            >Get Secure! -- www.microsoft.com/security
            >This posting is provided "as is" with no warranties and[/color]
            confers no rights.[color=blue]
            >
            >.
            >[/color]

            Comment

            • Tian Min Huang

              #7
              RE: WebResponse.Get Response Error

              Hi Jonathan,

              Thanks for your feedback.
              [color=blue][color=green]
              >> Isn't this a bug in WebRequest?[/color][/color]
              Based on my experience, WebRequest only gets the proxy information from IE
              settings and does not check what the active connection is.
              [color=blue][color=green]
              >> I see that the code that you sent would clear the proxy but how would I[/color][/color]
              detect that the user is has a DUN connection and then how do I know if they
              don't have proxy settings on the DUN.

              Though you can call GetAdaptersInfo () from you .NET managed application via
              P/Invoke to check whether the computer connects via LAN or DUN, I strongly
              recommend you catch the exception and try using WebRequest without proxy to
              work around this problem.

              I look forward to your feedback.

              Have a nice day!

              Regards,

              HuangTM
              Microsoft Online Partner Support
              MCSE/MCSD

              Get Secure! -- www.microsoft.com/security
              This posting is provided "as is" with no warranties and confers no rights.

              Comment

              Working...