代码之家  ›  专栏  ›  技术社区  ›  black sensei

如何通过代理服务器将WinForm连接到Internet

  •  2
  • black sensei  · 技术社区  · 16 年前

    我正在用C和.NET 2.0编写一个使用WebServices的桌面应用程序,并且我正在为当应用程序位于代理服务器之后时的情况做准备,比如 the example in this question .

    这是一个很好的主意,将以相同的方式设置app.config,但现在我想通过提供用户名和密码来测试连接。我拥有的代理服务器只是为了测试,并不妨碍我连接到互联网。

    我意识到,像NetBeans或Visual Studio这样的应用程序——举几个例子来说——通过在选项或首选项表单中提供一个完整的部分来认真对待代理,我也希望这样做。我也在读到通过socks4或socks5实现连接有点困难。你能分享你的知识吗?

    谢谢你的阅读。

    1 回复  |  直到 12 年前
        1
  •  2
  •   Robert Harvey    12 年前

    通过代理服务器连接到Web服务
    http://www.codeproject.com/KB/webservices/web_service_by_proxy.aspx

    示例代码:

    ' 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