我正在使用C#HttpListener类来实现一些服务器.这就是问题所在.我只想向客户端发送一个空的响应请求
HTTP/1.1 200 OK
Run Code Online (Sandbox Code Playgroud)
要么
HTTP/1.1 400 Bad Request
Run Code Online (Sandbox Code Playgroud)
没有任何额外的文字.所以我设置状态代码和状态描述,不要将任何字节写入响应OutputStream - 我只是不需要它们.然后关闭响应以使用response.Close()方法启动向客户端发送字节.而我在Fiddler所展示的客户端上得到的是
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Server: Microsoft-HTTPAPI/2.0
Date: Sun, 25 Oct 2015 10:42:12 GMT
0
Run Code Online (Sandbox Code Playgroud)
服务器和日期字段有一个解决方法 - HttpListener Server Header c#.
但是如何从这个响应中删除这些"Transfer-Encoding:chunked"artefact和"0"body?!
感谢所有提前!
代码:
private void ProcessContext(HttpListenerContext aContext)
{
HttpListenerResponse response = aContext.Response;
response.StatusCode = (int)HttpStatusCode.OK;
response.StatusDescription = "OK";
response.Close();
}
Run Code Online (Sandbox Code Playgroud) 我正在使用HttpClient类将一些数据发送到特定主机.我只想发送一个没有任何额外行的纯标题,如("Host:http").所以这一行是最后一个从标题中删除,但我不知道如何.
代码:
HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Post, aUrl);
msg.Headers.Clear();
msg.Headers.Remove("Host");
msg.Headers.ExpectContinue = false;
Encoding encoding = ConfiguratorASUST.Instance.Encoding ?? Encoding.GetEncoding(ConfiguratorASUST.ENCODING_DEFAULT);
msg.Content = new StringContent(aStr, encoding);
_client.SendAsync(msg);
Run Code Online (Sandbox Code Playgroud)
Fiddler中的结果标题:
POST http://http//localhost.fiddler:60001/POS/POSTELESPIS HTTP/1.1
Content-Type: text/plain; charset=windows-1251
Host: http
Run Code Online (Sandbox Code Playgroud)
Host: http需要从邮件的标题中删除此行.但是我怎么能这样做呢?!我尝试了以下方法:
msg.Headers.Clear();
msg.Headers.Remove("Host");
Run Code Online (Sandbox Code Playgroud)
无济于事.实际上我也看到了标题Proxy-Connection: Keep-Alive的添加.