C# 通过 HTTP Post 将 XML 发送到 IP:Port
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1099021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send XML via HTTP Post to IP:Port
提问by Steven
Ok so to start off, I'm not using any sort of web service. Right now I don't know a whole lot about the application receiving the XML other than it receives it. Big help there I know. I didn't write the receiving application but my company doesn't have any useful ways of testing the XML transmission phase.
好的,首先,我没有使用任何类型的网络服务。现在,除了接收 XML 之外,我对接收 XML 的应用程序知之甚少。我知道那里有很大的帮助。我没有编写接收应用程序,但我的公司没有任何有用的方法来测试 XML 传输阶段。
I basically want to send an XML document like this...
我基本上想发送这样的 XML 文档...
<H2HXmlRequest class="myClass">
<Call>
<CallerID></CallerID>
<Duration>0</Duration>
</Call>
<Terminal>
<CancelDate></CancelDate>
<ClerkLoginTime></ClerkLoginTime>
</Terminal>
<Transaction>
<AcceptedCurrency></AcceptedCurrency>
<AccountId>6208700003</AccountId>
</Transaction>
</H2HXmlRequest>
...to the application that I don't really know a whole lot about. It's nothing fancy and with the proper help I could probably find out more info. But what I am looking to do is to come up with some kind of C# Forms app that can take that request above, send it on over using an IP and port, and hopefully see something happen.
...对于我不太了解的应用程序。这没什么特别的,如果有适当的帮助,我可能会找到更多信息。但我想要做的是想出某种 C# Forms 应用程序,它可以接受上面的请求,使用 IP 和端口发送它,并希望看到一些事情发生。
采纳答案by Jeff Meatball Yang
The recommended way to make simple web requests is to use the WebClient object.
发出简单 Web 请求的推荐方法是使用 WebClient 对象。
Here's a code snippet:
这是一个代码片段:
// assume your XML string is returned from GetXmlString()
string xml = GetXmlString();
// assume port 8080
string url = new UriBuilder("http","www.example.com",8080).ToString();
// create a client object
using(System.Net.WebClient client = new System.Net.WebClient()) {
// performs an HTTP POST
client.UploadString(url, xml);
}
回答by user391318
If you have an IP and port why you are not trying XML over TCP/IP. In C# you can do this by using System.Net.Sockets class TCPClient. This class is having methods Connect , send and receive, in order to connect with IP and port then send message and wait to receive message.
如果您有 IP 和端口,为什么不尝试通过 TCP/IP 使用 XML。在 C# 中,您可以使用 System.Net.Sockets 类 TCPClient 来完成此操作。该类具有 Connect 、发送和接收方法,以便与 IP 和端口连接,然后发送消息并等待接收消息。