How to programmatically send information to a web service in C # with .NET?

I know that kind of considerations like reinventing the wheel here, but I need to know to communicate with the web service through http / soap / xml and web posts. The reason is that I need to communicate with a third-party web service to work, but something is wrong with WSDL or something, and it does not work when connected to it using the .NET wizard.

So can anyone give me a process / simple example / etc. how to do this or can someone give me a link to what explains this? I am not very good at web requests and responses.

How do I create and submit a request? How to analyze the response?

Here is the code for a simple web service. Pretend the address .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 + "!"; } } } 

What can I call this method?

Any help is appreciated.

EDIT

I just want to know how to send data to a web service. I can get all the action data / url of the / SOAP method, and I can parse the response data. I just don't know which objects to use or how to use them.

If anyone knows of a few simple soap.NET clients, such as SUDS in Python, this will help too.

+6
c # web-services webrequest
source share
4 answers

If you want to communicate directly, I would look at using HTTPWebRequest, since ultimately the webservice call is just sent XML using HTTP POST.

The following link has some examples: http://geekswithblogs.net/marcel/archive/2007/03/26/109886.aspx

As a way to test an external web service before contacting it programmatically with .net, one way is to use a test tool such as SOAPUI to create accurate XML that you think should be sent to the web service and send it manually using this tool.

Then you can develop the .net equivalent

EDIT is the quick example I knocked out to invoke your service example (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 was created from Jon Skeet.

+6
source share

The code of the selected answer did not work for me. I had to add SOAPAction to the header as well as modify the ContentType .

Here is the whole code:

 var strRequest = @"<soap12:Envelope> ... </soap12:Envelope>"; string webServiceUrl = "http://localhost:8080/AccontService.svc"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webServiceUrl); request.Method = "POST"; request.ContentType = "text/xml;charset=UTF-8"; request.Accept = "text/xml"; request.Headers.Add("SOAPAction", "http://tempuri.org/IAccountService/UpdateAccount"); byte[] data = Encoding.UTF8.GetBytes(strRequest); request.ContentLength = data.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(data, 0, data.Length); requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string responseXmlString = reader.ReadToEnd(); return new HttpResponseMessage() { Content = new StringContent(responseXmlString, Encoding.UTF8, "application/xml") }; 
+1
source share

There is XML-RPC.NET that allows you to create bindings on the fly.

eg. (example from their website):

 [XmlRpcUrl("http://betty.userland.com/RPC2")] public interface IStateName : IXmlRpcProxy { [XmlRpcMethod("examples.getStateName")] string GetStateName(int stateNumber); } 
0
source share

If your service was really as simple as your example, just use the "Add service link" and use a proxy.

If this does not work, use the svcutil.exe command-line tool and post the error messages it prints.

Do not use WSDL.EXE unless you have a choice.

0
source share

All Articles