我一直在想如何通過TCP服務器發送和接收XML數據。我來自java編程背景,所以我在這裏深入一點。我的程序工作,如果我只發送純文本,但是一旦我嘗試發送它只是掛起的XML數據。服務器從不接收消息。 我一直在尋找代碼來做到這一點,並沒有找到任何運氣,我已經看到很多在線代碼示例不起作用。如果您有任何人可以解決這個問題,我會非常感激。通過TCP發送和接收XML數據
請在這裏尋找代碼示例,而不是解釋我應該怎麼做來解決它。我只編寫了C#幾天。 這是示例XML請求。
<?xml version="1.0" encoding="utf-8"?>
<ClientRequest>
<Product>AGENT</Product>
<Method>GET_SYSTEM_INFO</Method>
<ClientId>UMOHB</ClientId>
<Params>
<Param Value="umohb" Key="username" />
<Param Value="password" Key="password" />
<Param Value="localhost" Key="hostname" />
</Params>
</ClientRequest>
這是我的TCP客戶端代碼
public static void sendStringRequest(String hostname, int port, String message)
{
String response = String.Empty;
TcpClient client = getConnection(hostname, port);
Console.WriteLine(message);
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
writer.AutoFlush = false;
writer.Write(Encoding.UTF8.GetBytes(message).Length);
writer.Write(message);
writer.Flush();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
response = reader.ReadLine();
stream.Close();
}
@JohnSaunders謝謝你,你的輸入,非常感謝......你有沒有想過,爲什麼你要在你的問題或響應添加代碼示例的選項? – greenkode 2012-02-20 15:38:37