C# HTTP错误400。使用TcpClient时,请求谓词无效

C# HTTP错误400。使用TcpClient时,请求谓词无效,c#,tcpclient,networkstream,C#,Tcpclient,Networkstream,我正在使用Visual Studio 2017和VC#并尝试连接到服务器机器 如果我使用带有此url的web浏览器: 我收到的答复如下: <Response> <Error>Transaction 1 is found.</Error> </Response> GET /url HTTP/1.1 Host: www.servername.com Accept: image/gif, image/jpeg, */* Accept-Lan

我正在使用Visual Studio 2017和VC#并尝试连接到服务器机器

如果我使用带有此url的web浏览器:

我收到的答复如下:

<Response>
     <Error>Transaction 1 is found.</Error>
</Response>
GET /url HTTP/1.1
Host: www.servername.com
Accept: image/gif, image/jpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
我在responseData中得到的答复是:

HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 26 Apr 2018 23:02:25 GMT
Connection: close
Content-Length: 326

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Verb</h2>
<hr><p>HTTP Error 400. The request verb is invalid.</p>
</BODY></HTML>
HTTP/1.1400错误请求
内容类型:text/html;字符集=美国ascii码
服务器:Microsoft HTTPAPI/2.0
日期:2018年4月26日星期四23:02:25 GMT
连接:关闭
内容长度:326
错误的请求
错误的请求-无效的动词
HTTP错误400。请求谓词无效

我错过了什么

问候
rubenc

当服务器用其“错误请求”响应代码告诉您时,您没有发送请求。典型的HTTP GET请求如下所示:

<Response>
     <Error>Transaction 1 is found.</Error>
</Response>
GET /url HTTP/1.1
Host: www.servername.com
Accept: image/gif, image/jpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
上面的每一行都应该以回车和换行符对(
\r\n
)结束,整个请求应该以空行(
\r\n
)结束


也就是说,除非这纯粹是一个学习练习,否则您几乎肯定不应该自己编写代码。相反,利用内置API或类似的功能。在当今时代,HTTP是包括C#/.NET在内的许多编程环境中的“一流公民”。

您真的需要使用TcpClient吗?WebRequest被禁止?哈哈我会使用WebRequest。。。谢谢好的,很好。但是,您缺少了许多可以从研究所有这些RFC和正确的HTTP格式等内容中收集到的有用信息:)