5

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.

I basically want to send an XML document like this...

<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.

Steven
  • 691
  • 3
  • 10
  • 26
  • You're mixing lot of concepts here. Title says http POST but question mentions Forms app and sending it via a TCP port. So which is it? a web app or WinForms app? And is the XML just something someone would paste in there? – Knut Haugen Jul 08 '09 at 16:13
  • No, it's WinForms App that can do a HTTP Post. – Steven Jul 08 '09 at 16:29

2 Answers2

10

The recommended way to make simple web requests is to use the WebClient object.

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);  

}
Jeff Meatball Yang
  • 37,839
  • 27
  • 91
  • 125
  • Should that be System.Net instead of System.Web? – Steven Jul 08 '09 at 16:31
  • Don't forget to either remove the `encoding` attribute from the xml declaration (e.g. ``), or make the encoding attribute match the encoding of the `WebClient.Encoding` property (e.g. ``) **Note:** i don't know what the default encoding of `WebClient` is, but you best hope it is some form of Unicode. – Ian Boyd Aug 24 '10 at 04:34
0

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.

sth
  • 222,467
  • 53
  • 283
  • 367
user391318
  • 61
  • 6
  • 2
    Why not? You shouldn't re-invent the wheel. There's already a library of tested, security checked, maintained code to send data to an http server. As interesting as it would be to write sockets code yourself, it's far too easy to have bugs, or security holes in it. – Ian Boyd Aug 24 '10 at 04:38