zoukankan      html  css  js  c++  java
  • C# 通过HTTP代理访问Socket来获取邮件

    C# 通过HTTP代理访问Socket来获取邮件

    关键穿透代理的代码(通过HTTP代理获取TcpClent)

    public class ClientHelper
        {
            public static TcpClient connectViaHTTPProxy(
                string targetHost,
                int targetPort,
                string httpProxyHost,
                int httpProxyPort,
                string proxyUserName,
                string proxyPassword)
            {
                var uriBuilder = new UriBuilder
                {
                    Scheme = Uri.UriSchemeHttp,
                    Host = httpProxyHost,
                    Port = httpProxyPort
                };
    
                var proxyUri = uriBuilder.Uri;
    
                var request = WebRequest.Create(
                    "http://" + targetHost + ":" + targetPort);
    
                var webProxy = new WebProxy(proxyUri);
    
                request.Proxy = webProxy;
                request.Method = "CONNECT";
    
                var credentials = new NetworkCredential(
                    proxyUserName, proxyPassword);
    
                webProxy.Credentials = credentials;
    
                var response = request.GetResponse();
    
                var responseStream = response.GetResponseStream();
                Debug.Assert(responseStream != null);
    
                const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;
    
                var rsType = responseStream.GetType();
                var connectionProperty = rsType.GetProperty("Connection", Flags);
    
                var connection = connectionProperty.GetValue(responseStream, 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 };
            }
        }
    

     原文地址:http://www.replicator.org/journal/201011170043-net-connecting-a-tcpclient-through-an-http-proxy-with-authentication

  • 相关阅读:
    Coding.net进阶,使用Git管理代码
    经典算法问题
    浅谈三款常用软件
    Coding.net简单使用指南
    湖北宜化总结
    天顺风能经验总结
    Vue中watch的高级用法
    html 锚点三种实现方法
    【机器学习】EM算法详细推导和讲解
    【机器学习】BP神经网络实现手写数字识别
  • 原文地址:https://www.cnblogs.com/mxm2005/p/4589098.html
Copyright © 2011-2022 走看看