| Author: seema 13 Oct 2009 | Member Level: Gold | Rating:  Points: 2 |
how to secure a webservice with example ??: http://msdn.microsoft.com/en-us/library/aa302428.aspx
|
| Author: seema 13 Oct 2009 | Member Level: Gold | Rating:  Points: 2 |
How to call a webservice dynamically ??
Step 1.
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class CustomerWebService : System.Web.Services.WebService { [WebMethod] public string Register(long id, string data1) { return "ID.CUSTOMER"; } }
Step 2.
POST /WebServices/CustomerWebService.asmx HTTP/1.1 Host: localhost Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://tempuri.org/Register"
<?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://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Register xmlns="http://tempuri.org/"> <id>long</id> <data1>string</data1> </Register> </soap:Body> </soap:Envelope>
Step 3. Create HttpWebRequest passing the WS url and soap action (similar to method name) and execute the request.
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://schemas.xmlsoap.org/soap/envelope/""> <soap:Body> <Register xmlns=""http://tempuri.org/""> <id>123</id> <data1>string</data1> </Register> </soap:Body> </soap:Envelope>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/WebServices/CustomerWebService.asmx"); req.Headers.Add("SOAPAction", "\"http://tempuri.org/Register\""); req.ContentType = "text/xml;charset=\"utf-8\""; req.Accept = "text/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(); // TODO: Do whatever you need with the response
|