Search code examples
c#winforms.net-2.0proxy

How to connect a winform to internet through a proxy server


I'm writing a desktop application in C# and .Net 2.0 that consumes webservices and i'm making provision for the case when the app is behind a proxy server, like with the example in this question.

It's a great idea and will set my app.config the same way, but right now I want to test the connection by providing the username and password. The proxy server I have is just for testing and doesn't really prevent me connecting to the internet.

I realized that applications like Netbeans or Visual Studio — to cite a few — take that proxy stuff seriously by providing a whole section in their options or preference forms and I want to do the same. I'm also reading that implementing the connection through socks4 or socks5 is kinda tough. So can you share your knowledge about that?

Thanks for reading.


Solution

  • Connect to a web service through a Proxy Server
    http://www.codeproject.com/KB/webservices/web_service_by_proxy.aspx

    Example code:

    ' Search button: do a search, display number of results 
    Private Sub btnSearch_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnSearch.Click 
    
    ' Create a Google Search object 
    Dim s As New Google.GoogleSearchService 
    
    Try 
    
    ' google params 
    Dim strLicenceseKey As String = "google license key" ' google license key 
    Dim strSearchTerm As String = "Bruno Capuano" ' google license key 
    
    ' proxy settings 
    Dim cr As New System.Net.NetworkCredential("user", "pwd", "MyDomain") 
    Dim pr As New System.Net.WebProxy("127.0.1.2", 80) 
    
    pr.Credentials = cr 
    s.Proxy = pr 
    
    ' google search
    Dim r As Google.GoogleSearchResult = s.doGoogleSearch(strLicenceseKey, _
      strSearchTerm, 0, 10, True, "", False, "", "", "")
    ' Extract the estimated number of results for the search and display it
    Dim estResults As Integer = r.estimatedTotalResultsCount 
    
    MsgBox(CStr(estResults))
    
    Catch ex As System.Web.Services.Protocols.SoapException
    
    MsgBox(ex.Message)
    
    End Try
    
    End Sub