如何查看在c#中发送的请求

时间:2013-12-19 13:45:09

标签: c# xml tcp response

我正在处理以下代码。

            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>

似乎附加了一些额外的内容。如何查看正在发送的请求,以便我可以更正?

2 个答案:

答案 0 :(得分: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()调用!

答案 1 :(得分: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);