@martensit

Как правильно запустить AuthenticateAsClient через http прокси?

var client = new TcpClient("192.168.56.2", 3128);
var stream = new SslStream(client.GetStream());
stream.AuthenticateAsClient("2ip.ru");
string request = "GET https://2ip.ru HTTP/1.1" + Environment.NewLine +
"Host: 2ip.ru" + Environment.NewLine +
"Connection: keep-alive" + Environment.NewLine + Environment.NewLine;
byte[] requestBytes = Encoding.ASCII.GetBytes(request);
stream.Write(requestBytes, 0, requestBytes.Length);
byte[] buffer = new byte[2048];
int bytes;
do
{
    bytes = stream.Read(buffer, 0, buffer.Length);
    Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
} while (bytes != 0);

Застревает на AuthenticateAsClient как раз.
Если спрашивать у ("2ip.ru", 443) вместо ("192.168.56.2", 3128), отвечает нормально.
AuthenticateAsClient через прокси нужно как то организовать правильно
  • Вопрос задан
  • 26 просмотров
Решения вопроса 1
@martensit Автор вопроса
public static TcpClient ProxyTcpClient(string targetHost, int targetPort, string httpProxyHost, int httpProxyPort)
{
    const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;
    Uri proxyUri = new UriBuilder
    {
        Scheme = Uri.UriSchemeHttp,
        Host = httpProxyHost,
        Port = httpProxyPort
    }.Uri;
    Uri targetUri = new UriBuilder
    {
        Scheme = Uri.UriSchemeHttp,
        Host = targetHost,
        Port = targetPort
    }.Uri;
    WebProxy webProxy = new WebProxy(proxyUri, true);
    //webProxy.Credentials = new NetworkCredential(proxyUserName, proxyPassword);
    WebRequest request = WebRequest.Create(targetUri);
    request.Proxy = webProxy;
    request.Method = "CONNECT";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    Type responseType = responseStream.GetType();
    PropertyInfo connectionProperty = responseType.GetProperty("Connection", Flags);
    var connection = connectionProperty.GetValue(responseStream, null);
    Type connectionType = connection.GetType();
    PropertyInfo networkStreamProperty = connectionType.GetProperty("NetworkStream", Flags);
    NetworkStream networkStream = (NetworkStream)networkStreamProperty.GetValue(connection, null);
    Type nsType = networkStream.GetType();
    PropertyInfo socketProperty = nsType.GetProperty("Socket", Flags);
    Socket socket = (Socket)socketProperty.GetValue(networkStream, null);
    return new TcpClient { Client = socket };
}

public static void Connect()
{
    TcpClient client = ProxyTcpClient("2ip.ru", 443, "192.168.56.2", 3128);
    var stream = new SslStream(client.GetStream());
    stream.AuthenticateAsClient("2ip.ru");
    string request = "GET https://2ip.ru HTTP/1.1" + Environment.NewLine +
    "Host: 2ip.ru" + Environment.NewLine +
    "Connection: keep-alive" + Environment.NewLine + Environment.NewLine;
    byte[] requestBytes = Encoding.ASCII.GetBytes(request);
    stream.Write(requestBytes, 0, requestBytes.Length);
    stream.Flush();
    var reader = new StreamReader(stream, Encoding.UTF8);
}

Вот решение
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
22 сент. 2019, в 15:19
500 руб./за проект
22 сент. 2019, в 14:31
300 руб./за проект
22 сент. 2019, в 14:31
4000 руб./за проект