I know this sort of counts as reinventing the wheel here, but I need to know to communicate with a web service through http/soap/xml and web messages. The reason is I need to communicate with a third party web service for work, but there is something wrong with the WSDL or something and it does not work when connecting to it with the .NET wizard.
So, can anyone give me a process/simple example/etc. of how to do this or can anyone give me a link to somewhere that explains it? I'm not very savvy with web requests and responses.
How do I construct and send the request? How do I parse the response?
Here is the code for a simple web service. Pretend the address of the .asmx is "http://www.mwebb.com/TestSimpleService.asmx":
using System;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace TestSimpleService
{
[WebService]
public class Soap : System.Web.Services.WebService
{
[WebMethod]
public string SayHello(string name)
{
return "Hello " + name + "!";
}
}
}
How would I call this method?
Any help is appreciated.
EDIT
I really just want to know how to send the data to the web service. I can get all of the method/SOAP action/URL data and I can parse the response data. I just don't know what objects to use or how to use them.
If anyone knows of a few simple .NET soap clients like SUDS in Python, that would help too.
To call a Web service programmaticallyUse the Web reference name (or server name) as the namespace, and the name of its . WSDL file (or the service name) as the proxy class. The following code example calls a Web service method to get a string value. Web service variable names and syntax are highlighted.
In the Project Type pane, select a Visual C# application. In the Templates pane, select a Windows Forms Application. Enter a project name such as CallingServiceExample , and browse to a storage location. In the Solution Explorer, right-click the project name and click Add Service Reference.
To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.
If you want to communicate directly, I'd look into using an HTTPWebRequest as ultimately a webservice call is just XML sent using an HTTP POST.
The following link has some examples: http://geekswithblogs.net/marcel/archive/2007/03/26/109886.aspx
As a way of testing the external webservice before contacting it programmatically with .net one way is to use a test tool like SOAPUI to produce the exact XML you think needs to be posted to the webservice and to send it manually with that tool
Then you can develop the .net equivalent
EDIT - here's a quick example I knocked up to call your example service (using SOAP1.2) based on the link above:
{
string soap = @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"">
<soap:Body>
<SayHello xmlns=""http://tempuri.org/"">
<name>My Name Here</name>
</SayHello>
</soap:Body>
</soap:Envelope>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:2439/Soap.asmx");
req.ContentType = "application/soap+xml;";
req.Method = "POST";
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(soap);
}
}
WebResponse response = req.GetResponse();
Stream responseStream = response.GetResponseStream();
// Do whatever you need with the response
Byte[] myData = ReadFully(responseStream);
string s = System.Text.ASCIIEncoding.ASCII.GetString(myData);
}
The ReadFully method comes from http://www.yoda.arachsys.com/csharp/readbinary.html and looks like it originated from Jon Skeet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With