Tcpclient 和 webRequest 的区别

Difference between Tcpclient and webRequest

我正在尝试验证网站是否正常运行。我正在使用 TcpClient 调用网站并且它工作正常但是当我对同一站点使用 webrequest 时它会抛出 404 错误。

TcpClient 和 webRequest 在功能上有什么区别??

var client = new TcpClient();
client.Connect("android.clients.google.com", 80);

var request = WebRequest.Create("http://android.clients.google.com");
var resp = request.GetResponse(); 

what is the difference in the functionality of TcpClient and webRequest??

用于与该服务器通信的 TcpClient is a wrapper around a TCP client socket, letting you communicate with any TCP server. The application protocol 将由您的代码实现。您的代码仅连接到 HTTP 服务器,但不与其通信。

一个 HttpWebRequest handles not only the TCP layer, but also the HTTP layer. This lets you execute web requests to any HTTP server. Your code executes a GET request to http://android.clients.google.com, which doesn't exist,因此您收到 404

为了解决您的原始问题:Web 服务器响应给定端口上的套接字连接甚至 returns HTTP 响应的事实,可能意味着也可能不意味着该站点被视为“已启动”,并且得到 200 响应也是如此。接收到 Twitter's "failwhale" 是否意味着该站点已“启动”?根据您实际尝试做的事情,可能需要不同的方法。有时最好是触发您想要的请求,而不是事先“ping”站点。

404错误表示网络服务器在线但没有该文件。错误是由网络服务器返回的,而不是在客户端。换句话说,测试是可以的。使用真正离线的 Web 服务器检查状态 - 您必须获得连接超时、端口关闭或类似信息。无论如何,为此目的最好使用 TcpClient。