System.Net.WebRequest under proxy environment

I am unable to communicate to web urls using system.net.webrequest under proxy enevironment.
I get "The underlying connection was closed: The remote name could not be resolved." error.
see below for sample code

Any help would be appreciated.

 I used both defaultcredentials and providing proxy user and password. I get the same error
Dim objRequest As Net.WebRequest
        Dim objStream As IO.Stream
        Dim objResponse As Net.WebResponse
        Dim objStreamReader As IO.StreamReader
        Dim retXML As String = ""
 
        Try
 
            objRequest = Net.WebRequest.Create(strURL)
            objRequest.Method = "POST"
            objRequest.Credentials = Net.CredentialCache.DefaultCredentials
 
            
                            objRequest.Proxy.Credentials = New NetworkCredential(m_strProxyUser, m_strProxyPass)
            
            If objData Is Nothing OrElse objData.Length = 0 Then
                objRequest.ContentLength = 0
            Else
 
                objRequest.ContentType = strContentType
                objRequest.ContentLength = objData.Length()
                objStream = objRequest.GetRequestStream
                objStream.Write(objData, 0, objData.Length)
                objStream.Flush()
                objStream.Close()
                objStream = Nothing
            End If
 
            ' geting respond XML
            objRequest.Timeout = CLng(1000) * 3600
            objStreamReader = New IO.StreamReader(objRequest.GetResponse().GetResponseStream, True)
 
            retXML = objStreamReader.ReadToEnd()
            objStreamReader.Close()
 
        Catch ex As Exception
            MsgBox("SendHTTPRequest" & ex.ToString)
        Finally
            objRequest = Nothing
            objStream = Nothing
            objResponse = Nothing
            objStreamReader = Nothing
        End Try
 
        Return retXML

Open in new window

KakoroatAsked:
Who is Participating?
 
MitzsConnect With a Mentor Commented:
we had the same issue and this his how we got around.

 
Dim MyWebProxy As WebProxy = New WebProxy(ConfigurationManager.AppSettings("ProxyAddress").ToString())
MyWebProxy.BypassProxyOnLocal = True
MyWebProxy.Credentials = New NetworkCredential(ConfigurationManager.AppSettings("ProxyUsername").ToString, ConfigurationManager.AppSettings("ProxyPassword").ToString, ConfigurationManager.AppSettings("ProxyDomain").ToString)
Dim objWebRequest As System.Net.HttpWebRequest
Dim objWebResponse As System.Net.HttpWebResponse
 
objWebRequest = CType(System.Net.WebRequest.Create(UrlRequestStringBuilder.ToString()), System.Net.HttpWebRequest)
objWebRequest.Proxy = MyWebProxy
objWebRequest.PreAuthenticate = True
objWebRequest.Accept = "*/*"
objWebRequest.ContentType = "text/xml"
objWebRequest.AllowAutoRedirect = True
objWebRequest.UserAgent = "Mozilla/4.0 (compatible;" + " MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)"
objWebResponse = CType(objWebRequest.GetResponse(), System.Net.HttpWebResponse)
0
 
KakoroatAuthor Commented:
Thanks for the answer. It worked.

I still dont understand why on some proxy environments, proxyaddress is not required. It just works with proxyuser and proxypassword.

Any idea why?

0
 
KakoroatAuthor Commented:
Thanks for the answer. It worked.

I still dont understand why on some proxy environments, proxyaddress is not required. It just works with proxyuser and proxypassword.

Any idea why?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.