Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# HttpListener. Sending EMPTY response (without any artefacts)

Tags:

c#

I am using C# HttpListener class to realize some server. And here is the problem. I just want to send to client an empty response to the request like

HTTP/1.1 200 OK

or

HTTP/1.1 400 Bad Request

without any additional text. So I set status code and status description and don't write any bytes to response OutputStream - I just don't need them. Then close the response to initiate sending bytes to the client with response.Close() method. And what I get on the client side shown by Fiddler is

HTTP/1.1 200 OK

Transfer-Encoding: chunked

Server: Microsoft-HTTPAPI/2.0

Date: Sun, 25 Oct 2015 10:42:12 GMT


0

There is a workaround for Server and Date fields - HttpListener Server Header c#.

But how to remove these "Transfer-Encoding: chunked" artefact and "0" body from this response?!

Thanks to all in advance!

The code:

private void ProcessContext(HttpListenerContext aContext)
    {
        HttpListenerResponse response = aContext.Response;

        response.StatusCode = (int)HttpStatusCode.OK;
        response.StatusDescription = "OK";
        response.Close();

}
like image 250
Pavel Avatar asked Oct 25 '15 11:10

Pavel


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

This will get rid of everything but the status and the Content-Length header:

HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:5555/");
listener.Start();
listener.BeginGetContext(ar =>
{
    HttpListener l = (HttpListener)ar.AsyncState;
    HttpListenerContext context = l.EndGetContext(ar);

    context.Response.Headers.Clear();
    context.Response.SendChunked = false;
    context.Response.StatusCode = 200;
    context.Response.Headers.Add("Server", String.Empty);
    context.Response.Headers.Add("Date", String.Empty);
    context.Response.Close();
}, listener);

and in fiddler you'll see this:

enter image description here

like image 185
Nasreddine Avatar answered Sep 24 '22 08:09

Nasreddine