System.Net.WebRequest under proxy environment

Posted on 2008-11-11
Last Modified: 2012-06-21
I am unable to communicate to web urls using 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 = ""


            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


                objRequest.ContentType = strContentType

                objRequest.ContentLength = objData.Length()

                objStream = objRequest.GetRequestStream

                objStream.Write(objData, 0, objData.Length)



                objStream = Nothing

            End If

            ' geting respond XML

            objRequest.Timeout = CLng(1000) * 3600

            objStreamReader = New IO.StreamReader(objRequest.GetResponse().GetResponseStream, True)

            retXML = objStreamReader.ReadToEnd()


        Catch ex As Exception

            MsgBox("SendHTTPRequest" & ex.ToString)


            objRequest = Nothing

            objStream = Nothing

            objResponse = Nothing

            objStreamReader = Nothing

        End Try

        Return retXML

Open in new window

Question by:Kakoroat
    LVL 4

    Accepted Solution

    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)

    Author Closing Comment

    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?


    Author Comment

    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now