如何与TcpClient.ConnectAsync()一起使用代理? [英] How to use Proxy with TcpClient.ConnectAsync()?

查看:102
本文介绍了如何与TcpClient.ConnectAsync()一起使用代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET中的HTTP代理支持实际上并不支持TcpClient或Socket等较低级别的类.但是我想通过支持"CONNECT"命令的HTTP代理连接TCPServer(IP,端口).

HTTP proxy support in .NET does not actually support the lower level classes like TcpClient or Socket. But I want to connect a TCPServer (ip, port) through HTTP proxy that support 'CONNECT' command.

所以我需要执行以下步骤:

So I need to do the following steps:

  1. 连接到代理.
  2. 发送 CONNECT Host:Port HTTP/1.1< CR>< LF>
  3. 发送< CR>< LF>
  4. 等待响应.如果它包含 HTTP/1.X 200 ,则说明连接成功.
  5. 阅读更多响应行,直到收到空白行.
  6. 它通过代理连接到外界.可以与代理进行任何数据交换.
  1. Connect to proxy.
  2. Send CONNECT Host:Port HTTP/1.1<CR><LF>
  3. Send <CR><LF>
  4. Wait for a line of response. If it contains HTTP/1.X 200, the connection is successful.
  5. Read further lines of response until receive an empty line.
  6. It's connected to the outside world through a proxy. Any data exchange as posssible with proxy.

实际上我是在没有代理的情况下完成的

Actually I do this without proxy

    TcpClient _client;
    NetworkStream _stream;

    public static async Task<bool> ConnectAsync(string hostname, int port)
    {
        _client = new TcpClient();
        await _client.ConnectAsync(hostname, port).ConfigureAwait(false);
        _stream = conn._client.GetStream();

        ..... Do some stuff

        // Connexion OK
        return true;
    }

在连接TcpClient之前如何使用代理和凭据?

How can use proxy and credentials before connecting TcpClient?

推荐答案

我在.NET:通过具有身份验证的HTTP代理连接TcpClient

这篇关于如何与TcpClient.ConnectAsync()一起使用代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆