问题
I write a very simple HTTP server by myself, and I use the C# Windows Form to retrieve the content of my HTTP server. My C# program always said protocol violation, CR must be followed by LF. I know this issue can be solved by adding Configuration Files to C# Projects. But I want to know the exactly reason. My http server code is in below.
/*
 * This will handle connection for each client
 * */
void *httpconnection_handler(void *socket_desc)
{
    //Get the socket descriptor
    int sock = *(int*)socket_desc;
    int read_size;
    char *message = "HTTP/1.1 200 OK\r\nContent-Type: text/xml; \r\ncharset=utf-8\r\n\r\n";
    char *end = "\r\n";
    char send_message[3000] = {0};
    //Send some messages to the client
    char * buffer = 0;
    long length;
    FILE * f = fopen ("/mnt/internal_sd/device-desc.xml", "r");
    if (f)
    {
      fseek (f, 0, SEEK_END);
      length = ftell (f);
      fseek (f, 0, SEEK_SET);
      buffer = malloc (length);
      if (buffer)
      {
        fread (buffer, 1, length, f);
      }
      fclose (f);
    }
    strcat(send_message, message);
    strcat(send_message, buffer);
    strcat(send_message, end);
    write(sock , send_message , length + strlen(message));
    sleep(1);
    shutdown(sock, SHUT_WR);
    //Free the socket pointer
    close(sock);
    return 0;
}
My C# code is in below.
using (WebClient webClient = new WebClient())
{
    webClient.Encoding = Encoding.UTF8;
    contents = webClient.DownloadString(url);
}
The exactly exception message is
RequestFailed: The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF
回答1:
It may be because of the \r\n you have inside of your Content-Type header.  The charset should be part of the Content-Type header line.
char *message = "HTTP/1.1 200 OK\r\nContent-Type: text/xml;charset=utf-8\r\n\r\n";
    来源:https://stackoverflow.com/questions/30228408/c-sharp-cr-must-be-followed-by-lf