static void Main(string[] args) { // create a new ProxySocket ProxySocket s = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // set the proxy settings s.ProxyEndPoint = new IPEndPoint(IPAddress.Parse("10.0.0.5"), 1080); s.ProxyUser = "******"; s.ProxyPass = "******"; s.ProxyType = ProxyTypes.Socks5; // if you set this to ProxyTypes.None, // the ProxySocket will act as a normal Socket // connect to the remote server // (note that the proxy server will resolve the domain name for us) s.Connect("www.mentalis.org", 80); // send an HTTP request s.Send(Encoding.ASCII.GetBytes("GET / HTTP/1.0\r\nHost: www.mentalis.org\r\n\r\n")); // read the HTTP reply int recv = 0; byte [] buffer = new byte[1024]; recv = s.Receive(buffer); while (recv > 0) { Console.Write(Encoding.ASCII.GetString(buffer, 0, recv)); recv = s.Receive(buffer); } // wait until the user presses enter Console.WriteLine("Press enter to continue..."); Console.ReadLine(); }
private SocksHttpWebResponse InternalGetResponse() { MemoryStream data = null; string header = string.Empty; using (var _socksConnection = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { var proxyUri = Proxy.GetProxy(RequestUri); var ipAddress = GetProxyIpAddress(proxyUri); _socksConnection.ProxyEndPoint = new IPEndPoint(ipAddress, proxyUri.Port); _socksConnection.ProxyType = ProxyTypes.Socks5; // open connection _socksConnection.Connect(RequestUri.Host, 80); // send an HTTP request _socksConnection.Send(Encoding.UTF8.GetBytes(RequestMessage)); // read the HTTP reply var buffer = new byte[1024 * 4]; int bytesReceived = 0; bool headerDone = false; while ((bytesReceived = _socksConnection.Receive(buffer)) > 0) { if (!headerDone) { var headPart = Encoding.UTF8.GetString(buffer, 0, bytesReceived > 1024 ? 1024 : bytesReceived); var indexOfFirstBlankLine = headPart.IndexOf("\r\n\r\n"); if (indexOfFirstBlankLine > 0) { headPart = headPart.Substring(0, indexOfFirstBlankLine); header += headPart; headerDone = true; var headerPartLength = Encoding.UTF8.GetByteCount(headPart) + 4; // 0123456789 // ---- if (headerPartLength < bytesReceived) { data = new MemoryStream(); data.Write(buffer, headerPartLength, bytesReceived - headerPartLength); } } else { header += headPart; } } else { if (data == null) { data = new MemoryStream(); } data.Write(buffer, 0, bytesReceived); } } if (data != null) { data.Position = 0; } } return new SocksHttpWebResponse(data, header); }
private SocksHttpWebResponse InternalGetResponse() { var response = new StringBuilder(); using (var _socksConnection = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { var proxyUri = Proxy.GetProxy(RequestUri); var ipAddress = GetProxyIpAddress(proxyUri); _socksConnection.ProxyEndPoint = new IPEndPoint(ipAddress, proxyUri.Port); _socksConnection.ProxyType = ProxyTypes.Socks5; // open connection _socksConnection.Connect(RequestUri.Host, 80); // send an HTTP request _socksConnection.Send(_correctEncoding.GetBytes(RequestMessage)); // read the HTTP reply var buffer = new byte[1024]; var bytesReceived = _socksConnection.Receive(buffer); while (bytesReceived > 0) { string chunk = _correctEncoding.GetString(buffer, 0, bytesReceived); string encString = EncodingHelper.GetEncodingFromChunk(chunk); if (!string.IsNullOrEmpty(encString)) { try { _correctEncoding = Encoding.GetEncoding(encString); } catch { //TODO: do something here } } response.Append(chunk); bytesReceived = _socksConnection.Receive(buffer); } } return new SocksHttpWebResponse(response.ToString(),_correctEncoding); }
static void Main(string[] args) { { // create a new ProxySocket ProxySocket s = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // set the proxy settings //s.ProxyEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9150); //s.ProxyUser = "******"; //s.ProxyPass = "******"; s.ProxyType = ProxyTypes.None; // if you set this to ProxyTypes.None, // the ProxySocket will act as a normal Socket // http://www.whatsmyip.org/ //<!-- Please DO NOT use our site to power an IP bot, script or other automated IP-lookup software! - for humans only! --> //<h1>Your IP Address is <span id="ip">169.120.138.139</span></h1> // connect to the remote server // (note that the proxy server will resolve the domain name for us) s.Connect("torguard.net", 80); // send an HTTP request s.Send(Encoding.ASCII.GetBytes("GET /whats-my-ip.php HTTP/1.0\r\nHost: torguard.net\r\n\r\n")); // read the HTTP reply int recv = 0; byte[] buffer = new byte[8096]; recv = s.Receive(buffer); while (recv > 0) { Console.Write(Encoding.ASCII.GetString(buffer, 0, recv)); recv = s.Receive(buffer); } } { // create a new ProxySocket ProxySocket s = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // set the proxy settings s.ProxyEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9150); //s.ProxyUser = "******"; //s.ProxyPass = "******"; s.ProxyType = ProxyTypes.Socks5; // if you set this to ProxyTypes.None, // the ProxySocket will act as a normal Socket // http://www.whatsmyip.org/ //<!-- Please DO NOT use our site to power an IP bot, script or other automated IP-lookup software! - for humans only! --> //<h1>Your IP Address is <span id="ip">169.120.138.139</span></h1> // connect to the remote server // (note that the proxy server will resolve the domain name for us) s.Connect("torguard.net", 80); // send an HTTP request s.Send(Encoding.ASCII.GetBytes("GET /whats-my-ip.php HTTP/1.0\r\nHost: torguard.net\r\n\r\n")); // read the HTTP reply int recv = 0; byte[] buffer = new byte[1024]; recv = s.Receive(buffer); while (recv > 0) { Console.Write(Encoding.ASCII.GetString(buffer, 0, recv)); recv = s.Receive(buffer); } } // wait until the user presses enter Console.WriteLine("Press enter to continue..."); Console.ReadLine(); }