I’m trying to interface with a web service. I am posting a SOAP Evelolpe and it returns a SOAP response.
I am able to post to the service and get the response using Web Request and Response.But I want to do that using WCF. Can some body please help me to achieve this.
My HTTP Post :
public string HttpPost (string uri, string parameters)
{
WebRequest webRequest = WebRequest.Create (uri);
webRequest.ContentType = "application/soap+xml; charset=utf-8";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes (parameters);
Stream os = null;
try
{ // send the Post
webRequest.ContentLength = bytes.Length;
os = webRequest.GetRequestStream();
os.Write (bytes, 0, bytes.Length); //Send it
}
try
{ // get the response
WebResponse webResponse = webRequest.GetResponse();
if (webResponse == null)
{ return null; }
StreamReader sr = new StreamReader (webResponse.GetResponseStream());
return sr.ReadToEnd ().Trim ();
}
return null;
}
Basically, what you need is:
(url of your service)?wsdl)Next: from Visual Studio, create a project, and then right-click on
Referencesin your solution explorer and then pickAdd Service Referencefrom the context menu.Either type in the URL (with the
?wsdl) in the dialog box, or type in the disk path where your WSDL/XSD files are stored.This will add a WCF service reference to that service to your project. You should now have an entry under
Service Reference– underneath what you see, there lie a couple of hidden files, which contain all the generated code now necessary to call that service.Basically, one of the file should be called
(name of your service)Client– and it’s in the namespace that you defined when adding the service reference (default isServiceReference1). Using that namespace, you should be able to create that WCF client now:With this WCF client, you should be able to call service methods easily, and pass in parameters (strings etc.) to those methods, and possibly also getting back responses (as a string, or as a class) from that service method.