2017-04-06 68 views
-1

我想要做的是用TcpClient與HTTP協議發送數據。但我在這裏面臨一個問題。我發送的數據(標題之後)並未完全發送到客戶端的應用程序(例如瀏覽器)。代碼:發送數據與HTTP和TcpClient未完全發送

// Building data. 
string notfound = "NOT FOUND"; 
byte[] data = Encoding.UTF8.GetBytes(notfound); 

// Building header. 
m_responseBuilder.Append("HTTP/1.1 404 Not Found").Append(CRLF); 
m_responseBuilder.Append("Server: ETP").Append(CRLF); 
m_responseBuilder.Append("Content-Type: text/plain").Append(CRLF); 
m_responseBuilder.Append("Content-Length: ").Append(data.Length.ToString()).Append(CRLF); 
m_responseBuilder.Append(CRLF); 
byte[] header = Encoding.UTF8.GetBytes(m_responseBuilder.ToString()); 

// Sending header. 
await SendDataAsync(header); 

// Sending data. 
await SendDataAsync(data); 

的SendDataAsync:

async Task SendDataAsync(byte[] data) 
    { 
     try 
     { 
      await m_tcpchannel.WriteAsync(data, 0, data.Length); 
     } 
     catch 
     { 
      DestroyConnection(); 
     } 
    } 

我得到了什麼:

NOT FOUN 

哪些錯誤就在這裏?

+0

1)爲什麼不使用HttpListener?和2)當然,它的破碎,你粘在你的手指在字節湯和攪拌它周圍。 (你不能UTF8編碼圖像;編碼是針對字符串的,這就是它在System.Text命名空間中的原因)。 –

+0

1)我想學習abt http數據包 –

+0

2)文件不編碼爲字符串,它直接發送爲字節 –

回答

0

是的,我得到了問題。這是CRLF。我寫了\ n \ r而不是\ r \ n ....