public override WebResponse GetResponse() { if (Proxy == null) { throw new InvalidOperationException("Proxy property cannot be null."); } if (String.IsNullOrEmpty(Method)) { throw new InvalidOperationException("Method has not been set."); } if (RequestSubmitted) { return _response; } _response = InternalGetResponse(); RequestSubmitted = true; return _response; }
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); } } SocksHttpWebResponse shwr= new SocksHttpWebResponse(response.ToString(),_correctEncoding); string httpstatuscode= shwr.Headers.Get("status"); string Location= shwr.Headers.Get("Location"); if (httpstatuscode.IndexOf("303")>-1) { _requestUri = new Uri( Location); RequestMessage = null; _requestMessage = null; shwr = InternalGetResponse(); } return shwr; }