2013-12-19 49 views
0

我正在處理以下代碼。如何查看在C中發送的請求#

  Int32 port = 7101; 
      IPAddress localAddr1 = IPAddress.Parse("117.239.85.13"); 
      MemoryStream ms = new MemoryStream(); 
      XmlDocument doc = new XmlDocument(); 
      String message_text = "<estel><header><requesttype>CREDIT</requesttype></header><request><agentcode>910000006789</agentcode><vendorcode>HELLOTV</vendorcode><pin>***************</pin><destination>919555518484</destination><agenttransid>*****</agenttransid><amount>10</amount><productcode>SKUA</productcode><clienttype>portal</clienttype><comments>abcd</comments></request></estel>"; 
      doc.LoadXml(message_text); 
      doc.Save(ms); 

      ms.Seek(0, SeekOrigin.Begin); 
      Byte[] data = ms.ToArray(); 

      TcpClient client = new TcpClient(); 
      client.Connect(localAddr1, port); 
      MessageBox.Show(client.Connected.ToString()); // Connection was established here. Returns TRUE value 

      NetworkStream stream = client.GetStream(); 
      StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); 
      writer.AutoFlush = false; 
      writer.Write(Encoding.UTF8.GetBytes(message_text).Length); 

      writer.Flush(); 
      stream.Write(data, 0, data.Length); 
      MessageBox.Show(stream.DataAvailable.ToString()); // Returns TRUE value 


      Byte[] bytes = new Byte[client.ReceiveBufferSize]; 
      string responseData; 
      stream.Read(bytes, 0, Convert.ToInt32(client.ReceiveBufferSize)); 
      responseData = Encoding.UTF8.GetString(bytes); // We are getting response here 
      stream.Close(); 
      client.Close(); 
      textBox1.Text = responseData.ToString(); 

這裏我使用IP和端口號向Web服務提供商發送XML數據。發送請求後,我收到以下響應15次

<estel><header><responsetype>INVALID REQUEST</responsetype></header></estel> 

看起來有些額外的內容正在追加。我如何查看發送的請求,以便我可以更正它?

回答

1

你期望發送什麼?

NetworkStream stream = client.GetStream(); 
StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); 
writer.AutoFlush = false; 
writer.Write(Encoding.UTF8.GetBytes(message_text).Length); 

它看起來像你在內容之前寫入XML文檔到流中的長度。

我希望你的信息流中正在把輸出是這樣的: 357<estel><header><requesttype>CREDIT</requesttype></header><request><agentcode>910000006789</agentcode><vendorcode>HELLOTV</vendorcode><pin>***************</pin><destination>919555518484</destination><agenttransid>*****</agenttransid><amount>10</amount><productcode>SKUA</productcode><clienttype>portal</clienttype><comments>abcd</comments></request></estel>

注意在開始數。這是文檔的長度。

你正在做大量的冗餘與MemoryStream。這不是必需的。對於一些更乾淨的代碼,刪除msdata和嘗試這個:

NetworkStream stream = client.GetStream(); 
StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); 
writer.AutoFlush = false; 
writer.Write(doc.Value.Length); //is this line intended? Take it out, see what happens! 
writer.Write(doc.Value); 
writer.Flush(); 

爲什麼這項工作? XmlNode.Value爲您提供節點的字符串內容。就你而言,這是整個文檔內容。您的StreamWriter設置了您想要使用的編碼,UTF-8。如果你傳遞一個字符串,它會爲你做Encoding.UTF8.GetBytes()電話!

0

我正在寫一個類似的程序,向ncell發送請求。我遇到同樣的問題,但我找到了解決方案。問題在於xml字符串格式。在telnet中測試xml請求的末尾需要一個新行。

String message_text = "<estel><header><requesttype>CREDIT</requesttype></header><request><agentcode>910000006789</agentcode> <vendorcode>HELLOTV</vendorcode><pin>***************</pin> <destination>919555518484</destination><agenttransid>*****</agenttransid> <amount>10</amount><productcode>SKUA</productcode> <clienttype>portal</clienttype><comments>abcd</comments></request></estel>" 
+ Environment.NewLine; 
    Byte[] data = Encoding.UTF8.GetBytes(message_text); 
    stream.Write(data, 0, data.Length);