Results 1 to 2 of 2

Thread: HttpWebRequest WebException

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    HttpWebRequest WebException

    I have a vb.net service that has a part where it calls a function that is built to take a url and get the source code using HttpWebRequest.

    On my development machine it works as expected. When I move the service and run on a Windows Server 2003 that has Internet Explorer 7 it returns the following:
    The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Authentication failed because the remote party has closed the transport stream.

    So on the server, I opened Internet Explorer 7, put the url in and sure enough the site doesn't pull up. Went to my Windows 7 pc with IE 11 and sure enough the site works as expected.

    Does HttpWebRequest use IE settings? I do have Firefox 36.0.1 on 2003 server and my url works in that as well so I need to either figure out how to make my code use the Firefox settings or see if I can get admins to update IE browser.

    Any ideas on if I can get this working with IE 7?

    Code:
    #Region " Public Request Functions/Subroutines "
        Public Function Request(ByVal sPOSTData As String, Optional ByVal bAutoRedirect As Boolean = False) As String
            Dim uriSite As Uri
            Dim sReturn As String = ""
            Dim srReader As StreamReader
            Dim sTemp As String = ""
    
            Try
                ' Setup request
                uriSite = New Uri(m_sURL)
                LogIt("+Start of Request function url: " & m_sURL)
    
                Try
                    m_hwrRequest = DirectCast(WebRequest.Create(uriSite), HttpWebRequest)
                Catch ex As Exception
                    LogItError("-HttpWebrequest error: " & ex.ToString)
                End Try
    
                If System.Configuration.ConfigurationManager.AppSettings("EnableProxy") = 1 Then
                    'Next 4 lines are for certain networks with proxy server running
                    m_hwrRequest.Proxy = WebRequest.DefaultWebProxy
                    m_hwrRequest.Proxy.Credentials = CredentialCache.DefaultCredentials
                    m_hwrRequest.AuthenticationLevel = Net.Security.AuthenticationLevel.None
                    m_hwrRequest.PreAuthenticate = False
                End If
    
                m_hwrRequest.Referer = m_sReferer
                m_hwrRequest.UserAgent = m_sUserAgent
                m_hwrRequest.AllowAutoRedirect = bAutoRedirect
                m_hwrRequest.AllowWriteStreamBuffering = True
                m_hwrRequest.KeepAlive = False
                'm_hwrRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*"
                m_hwrRequest.Accept = " text/html, application/xhtml+xml, */*"
    
                Try
                    'Set cookies if any
                    If m_ccCookies.Count > 0 Then
                        m_hwrRequest.CookieContainer = New CookieContainer()
                        m_hwrRequest.CookieContainer.Add(m_ccCookies)
    
                        LogIt("Pre Cookie")
                        For Each cc As Cookie In m_ccCookies
                            LogIt(cc.ToString)
                        Next
    
                    End If
                Catch ex As Exception
                    LogItError("-CookieContainer error: " & ex.ToString)
                End Try
    
                'If Not (hshHeader Is Nothing) = True Then
                '    For Each de As DictionaryEntry In hshHeader
                '        m_hwrRequest.Headers.Add(de.Key.ToString, de.Value.ToString)
                '    Next
                'End If
    
                If Not (sPOSTData Is Nothing) AndAlso sPOSTData.Length > 0 Then
                    Dim stWS As Stream
                    Dim aeEnc As ASCIIEncoding
                    Dim baBuf As Byte()
    
                    aeEnc = New ASCIIEncoding()
                    baBuf = aeEnc.GetBytes(sPOSTData)
    
                    m_hwrRequest.Method = "POST"
                    m_hwrRequest.ContentLength = baBuf.Length
                    m_hwrRequest.ContentType = "application/x-www-form-urlencoded"
    
                    stWS = m_hwrRequest.GetRequestStream()
                    stWS.Write(baBuf, 0, baBuf.Length)
                    stWS.Close()
                End If
    
                Try
                    m_hwrResponse = DirectCast(m_hwrRequest.GetResponse(), HttpWebResponse)
                Catch ex As Exception
                    LogItError("-HttpWebResponse error: " & ex.ToString)
                    If ex.ToString.IndexOf("connection was closed") Then
                        LogIt("Site appears to be down.")
                        Return "SITE DOWN"
                    End If
                End Try
    
                Try
                    srReader = New StreamReader(m_hwrResponse.GetResponseStream())
                    sReturn = srReader.ReadToEnd()
                    srReader.Close()
                Catch ex As Exception
                    LogItError("-Read HttpWebResponse error: " & ex.ToString)
                    If ex.ToString.IndexOf("connection was closed") Then
                        LogIt("Site appears to be down.")
                        Return "SITE DOWN"
                    End If
                End Try
    
    
                '------------------------------------------------------------------
                ' capture the redirect location from the header
                '------------------------------------------------------------------
    
                Try
                    'clear location
                    Location = String.Empty
    
                    Dim wbHCol As WebHeaderCollection = m_hwrResponse.Headers
                    Dim i As Integer
                    Dim iFound As Integer = 0
                    For i = 0 To wbHCol.Count - 1
    
                        Dim header As String = wbHCol.GetKey(i)
                        Dim values As String() = wbHCol.GetValues(header)
    
                        If values.Length > 0 AndAlso header.ToLower = "location" Then
                            Location &= values(0)
                            LogIt("Location: " & values(0))
                            iFound = 1
                        End If
                    Next
    
                    If iFound > 0 Then
                        LogIt("Request header since more then 1: " & m_hwrResponse.Headers.ToString)
                    End If
                Catch
                    Location = String.Empty
                End Try
                '------------------------------------------------------------------
    
                Try
                    If Not m_hwrResponse.Headers("Set-Cookie") Is Nothing Then
                        Dim ccContainer As New CookieContainer()
    
                        ccContainer = New CookieContainer()
                        ccContainer.SetCookies(m_hwrResponse.ResponseUri, m_hwrResponse.Headers("Set-Cookie"))
                        sTemp = m_hwrResponse.Headers("Set-Cookie").ToString
                        LogIt("Header cookies: " & sTemp)
                        m_ccCookies.Add(ccContainer.GetCookies(m_hwrResponse.ResponseUri))
    
                        LogIt("Post Cookie")
                        For Each cc As Cookie In m_ccCookies
                            LogIt(cc.ToString)
                        Next
                    End If
                Catch ex As Exception
                    LogItError("-set cookie header error: " & ex.ToString)
                End Try
    
                Me.Referer = m_hwrResponse.ResponseUri.AbsoluteUri
    
                'close response connection
                m_hwrResponse.Close()
    
            Catch ex As Exception
                'RaiseEvent RequestError(ex)
                LogItError("ERROR: Request function had the following error: " & ex.Message.ToString)
            End Try
    
            Return sReturn
        End Function

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,390

    Re: HttpWebRequest WebException

    The simple answer is yes it does use the settings. Can you maybe not fire up fiddler and view a successful request and then monitor ie7 attempt. Might shed some light....Guess only.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width