【问题标题】:Bypass the proxy using TcpClient使用 TcpClient 绕过代理
【发布时间】:2012-07-29 15:02:16
【问题描述】:

我正在尝试编写一个本地代理应用程序。我知道代理应用程序在理论上是如何工作的。 我已经完成了与处理传入连接相关的所有工作。但问题是我应该如何将客户端请求的请求发送到指定的 Url。当我尝试使用 TcpClient 创建与指定 URL 和端口的连接时,它会引发以下异常:

没有这样的主机是已知的

编辑:我想我应该绕过代理,比如 FireFox 正在做的甚至是系统代理集。

任何想法都会有所帮助。 提前致谢。

【问题讨论】:

标签: c# networking proxy tcpclient


【解决方案1】:

【讨论】:

  • 感谢您的回复,第一个链接帮助了我。我更改了几行代码,它起作用了。
【解决方案2】:

根据 colinsmith 提供的链接,我已经使用 TcpClient 绕过代理。这是我的做法:

    public static TcpClient CreateTcpClient(string url)
    {
        var webRequest = WebRequest.Create(url);
        webRequest.Proxy = null;

        var webResponse = webRequest.GetResponse();
        var resposeStream = webResponse.GetResponseStream();

        const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;

        var rsType = resposeStream.GetType();
        var connectionProperty = rsType.GetProperty("Connection", flags);

        var connection = connectionProperty.GetValue(resposeStream, null);
        var connectionType = connection.GetType();
        var networkStreamProperty = connectionType.GetProperty("NetworkStream", flags);

        var networkStream = networkStreamProperty.GetValue(connection, null);
        var nsType = networkStream.GetType();
        var socketProperty = nsType.GetProperty("Socket", flags);
        var socket = (Socket)socketProperty.GetValue(networkStream, null);

        return new TcpClient { Client = socket };
    }

希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2011-04-09
    • 2015-03-18
    相关资源
    最近更新 更多