在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论