C# 通过 .NET TcpClient 与 HTTP 代理通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/691550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Communicating with an HTTP Proxy via a .NET TcpClient
提问by
How can I communicate through an HTTP proxy with TcpClient
in C#, kind of like WebProxy
when using HttpWebResponse
?
我如何通过 HTTP 代理与TcpClient
C# 进行通信,有点像WebProxy
使用时HttpWebResponse
?
回答by Jon Skeet
Well, TCP doesn't have anything directly equivalent to HTTP proxying. In HTTP, the client (generally) knows about the proxying - it talks to the proxy, and asksthe proxy to connect to the real web server on its behalf.
嗯,TCP 没有任何直接等同于 HTTP 代理的东西。在 HTTP 中,客户端(通常)知道代理 - 它与代理交谈,并要求代理代表它连接到真实的 Web 服务器。
TCP doesn't define that sort of thing, so any proxying would have to either be transparent (i.e. something that a router or the operating system does without the client knowing, e.g. with iptables) or as part of the protocol on top of TCP (HTTP proxying is a good example of this, as is SOCKSmentioned in a different answer).
TCP 没有定义那种东西,所以任何代理都必须是透明的(即路由器或操作系统在客户端不知道的情况下所做的事情,例如使用iptables)或作为 TCP 之上的协议的一部分( HTTP 代理就是一个很好的例子,正如另一个答案中提到的SOCKS一样)。
回答by Greg
If you go down to low-level socket programming, I'm pretty sure you'll need to write your own proxy client. If you're only dealing with the HTTP protocol, you're probably better off using HTTP-specific classes. If you need to do it with sockets, the HTTP specdescribes the behavior of proxies reasonably well, so you could write your own client.
如果您进行低级套接字编程,我很确定您需要编写自己的代理客户端。如果您只处理 HTTP 协议,则最好使用特定于 HTTP 的类。如果您需要使用套接字来做这件事,HTTP 规范相当好地描述了代理的行为,因此您可以编写自己的客户端。