如何在c#中使用TCP / IP传递XML数据 [英] How to pass XML data using TCP/IP in c#

查看:760
本文介绍了如何在c#中使用TCP / IP传递XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#桌面应用程序,我正在尝试使用服务提供商提供的API连接到Web服务。 API包含服务器IP和端口号。要发送的请求是XML格式。虽然与服务器的连接已建立,但我在发送请求时遇到问题。



I am working on a C# Desktop Application and I am trying to connect to a web service using an API provided by the service provider. The API contains server IP and Port No.. The request which is to be sent is in XML format. I am having problem in sending the request, though the connection with the server is established.

Int32 port = 7101; // This is the port number
                IPAddress localAddr1 = IPAddress.Parse("117.239.85.13"); // This is the ip address
                TcpClient client = new TcpClient();
                client.Connect(localAddr1, port);
                MessageBox.Show(client.Connected.ToString()); // Here the status is True

//Following is the XML message

                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>xyz</comments></request></estel>";
                
                Byte[] data = System.Text.UTF8Encoding.ASCII.GetBytes(message_text);

                NetworkStream stream = client.GetStream();
                StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
                writer.AutoFlush = false;
                writer.Write(Encoding.UTF8.GetBytes(message_text).Length);
                writer.Write(message_text);
                writer.Flush();
                stream.Write(data, 0, data.Length);
                MessageBox.Show(stream.DataAvailable.ToString()); // Here I'm getting the status for DataAvailable as False


                Byte[] bytes = new Byte[client.ReceiveBufferSize];
                string responseData;
                stream.Read(bytes, 0, Convert.ToInt32(client.ReceiveBufferSize));
                responseData = Encoding.ASCII.GetString(bytes);
                stream.Close();
                client.Close();





请指导我。



Kindly guide me.

推荐答案

Saurav - 如果你因为xml而遇到问题,你可能想尝试这样的事情:



Saurav - If you are having problems because of the xml, you may want to try doing something like:

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>xyz</comments></request></estel>";
doc.LoadXml(message_text);
doc.Save(ms); // this should attach xml processing instructions, etc. And throw an exception if there's a problem with your xml
// then you should be able to use ms.ToArray() to get the byte array. You may have to call ms.Seek(0, SeekOrigin.Begin); first





然后用你的代码



Then use your code to


你好克里斯托弗 - 谢谢救命。我按照你的建议做了改动并得到了答复。
Hi Christopher - Thanks for the help. I made the changes as per your suggestion and got the response.


这篇关于如何在c#中使用TCP / IP传递XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆