Web Service - Query
-
Hi All I am looking for a solution to a problem regarding web service. I want to call a web method of a web service, but I don’t want to use wsdl nor can I generate the proxy class. The scenario is that, in my application user can fill in the web service address and the method he wants to execute and I have to get the results. I have some solution to this problem. Right now i am using WSE to solve this problem. My code is: Dim uriMe As Uri = New Uri("soap.tcp://localhost/TestWebService:80/MyService.asmx") Dim epMe As EndpointReference = New EndpointReference(uriMe) Dim receive As ReceiveData = New ReceiveData Dim sSend As SoapSender Dim env As SoapEnvelope Dim uriTo As Uri Dim epTo As EndpointReference receive.LoadTesterForm = Me SoapReceivers.Add(epMe, receive) sSend = New SoapSender env = New SoapEnvelope env.CreateBody() env.Context.Addressing.ReplyTo = New ReplyTo(uriMe) uriTo = New Uri(WebServiceURLTextBox.Text.Trim) epTo = New EndpointReference(uriTo) 'env.SetBodyObject("dosomething") 'env.Context.Addressing.To = New To(epTo.Address) env.Context.Addressing.Action = New Action("soap.tcp://localhost/TestWebService:80/MyService.asmx/HelloWorld") '"URI:http://localhost/TestWebService/MyService.asmx/HelloWorld") sSend.Destination = epTo ' CType(epTo, Microsoft.Web.Services2.Addressing.EndpointReference)() Try sSend.Send(env) Catch ex As Exception End Try Now i am getting an error with SOAP Action. What should we pass in a SOAP Action. If you could help me in this, that would be great. Waiting for reply, Thanks and Regards, Sumit Domyan
-
Hi All I am looking for a solution to a problem regarding web service. I want to call a web method of a web service, but I don’t want to use wsdl nor can I generate the proxy class. The scenario is that, in my application user can fill in the web service address and the method he wants to execute and I have to get the results. I have some solution to this problem. Right now i am using WSE to solve this problem. My code is: Dim uriMe As Uri = New Uri("soap.tcp://localhost/TestWebService:80/MyService.asmx") Dim epMe As EndpointReference = New EndpointReference(uriMe) Dim receive As ReceiveData = New ReceiveData Dim sSend As SoapSender Dim env As SoapEnvelope Dim uriTo As Uri Dim epTo As EndpointReference receive.LoadTesterForm = Me SoapReceivers.Add(epMe, receive) sSend = New SoapSender env = New SoapEnvelope env.CreateBody() env.Context.Addressing.ReplyTo = New ReplyTo(uriMe) uriTo = New Uri(WebServiceURLTextBox.Text.Trim) epTo = New EndpointReference(uriTo) 'env.SetBodyObject("dosomething") 'env.Context.Addressing.To = New To(epTo.Address) env.Context.Addressing.Action = New Action("soap.tcp://localhost/TestWebService:80/MyService.asmx/HelloWorld") '"URI:http://localhost/TestWebService/MyService.asmx/HelloWorld") sSend.Destination = epTo ' CType(epTo, Microsoft.Web.Services2.Addressing.EndpointReference)() Try sSend.Send(env) Catch ex As Exception End Try Now i am getting an error with SOAP Action. What should we pass in a SOAP Action. If you could help me in this, that would be great. Waiting for reply, Thanks and Regards, Sumit Domyan
Hi there, To send a message using WS-Addressing, you need to specify the web method name in the Action field, the sample code looks something like:
SoapEnvelope env = new SoapEnvelope();
env.Context.Addressing.Action = "http://tempuri.org/HelloWorld";env.CreateBody();
env.Body.InnerXml = String.Format("<HelloWorld xmlns=\"http://tempuri.org/\\" />");EndpointReference epr = new EndpointReference(new Uri("http://localhost/SimpleWSEService/Service1.asmx"));
SoapSender ss = new SoapSender(epr);
ss.Send(env);For more information, see Moving from WS-Routing to WS-Addressing Using WSE 2.0[^]
-
Hi there, To send a message using WS-Addressing, you need to specify the web method name in the Action field, the sample code looks something like:
SoapEnvelope env = new SoapEnvelope();
env.Context.Addressing.Action = "http://tempuri.org/HelloWorld";env.CreateBody();
env.Body.InnerXml = String.Format("<HelloWorld xmlns=\"http://tempuri.org/\\" />");EndpointReference epr = new EndpointReference(new Uri("http://localhost/SimpleWSEService/Service1.asmx"));
SoapSender ss = new SoapSender(epr);
ss.Send(env);For more information, see Moving from WS-Routing to WS-Addressing Using WSE 2.0[^]
Thanks a lot for your reply. My code for sending a request through SOAPSender is working fine now. But i am not getting any response back. Can you please tell me how get response through SOAPReceiver. Sumit Domyan
-
Thanks a lot for your reply. My code for sending a request through SOAPSender is working fine now. But i am not getting any response back. Can you please tell me how get response through SOAPReceiver. Sumit Domyan
Hi there, The SoapReceiver class is basically used to receive a message sent from a SoapSender object, so you cannot use it to receive the response result returned from the web method. First of all, you need to know that the WSE supports both a one-way messaging model with the SoapSender and SoapReceiver classes and a request/response pair model using the SoapClient and SoapService classes, you can take a look at Sending and Receiving SOAP Messages [^]for more information. So in this case, you may consider using the SoapClient instead to send the message in order to receive the response, the sample code looks something like
...
SoapEnvelope env = new SoapEnvelope();
env.Context.Addressing.Action = "http://tempuri.org/HelloWorld";
env.CreateBody(); // must create body before setting it
env.Body.InnerXml = String.Format("<HelloWorld xmlns=\"http://tempuri.org/\\" />");EndpointReference epr = new EndpointReference(new Uri("http://localhost/BasicWSEService/Service1.asmx"));
MyClient client = new MyClient(epr);
SoapEnvelope rs = client.SendRequestResponse(env);
string bodyResponse = rs.Body.OuterXml;
...class MyClient : SoapClient
{
public MyClient(EndpointReference destination) : base(destination)
{
}\[SoapMethod("HelloWorld")\] public SoapEnvelope SendRequestResponse(SoapEnvelope envelope) { return base.SendRequestResponse("HelloWorld",envelope); }
}
-
Hi there, The SoapReceiver class is basically used to receive a message sent from a SoapSender object, so you cannot use it to receive the response result returned from the web method. First of all, you need to know that the WSE supports both a one-way messaging model with the SoapSender and SoapReceiver classes and a request/response pair model using the SoapClient and SoapService classes, you can take a look at Sending and Receiving SOAP Messages [^]for more information. So in this case, you may consider using the SoapClient instead to send the message in order to receive the response, the sample code looks something like
...
SoapEnvelope env = new SoapEnvelope();
env.Context.Addressing.Action = "http://tempuri.org/HelloWorld";
env.CreateBody(); // must create body before setting it
env.Body.InnerXml = String.Format("<HelloWorld xmlns=\"http://tempuri.org/\\" />");EndpointReference epr = new EndpointReference(new Uri("http://localhost/BasicWSEService/Service1.asmx"));
MyClient client = new MyClient(epr);
SoapEnvelope rs = client.SendRequestResponse(env);
string bodyResponse = rs.Body.OuterXml;
...class MyClient : SoapClient
{
public MyClient(EndpointReference destination) : base(destination)
{
}\[SoapMethod("HelloWorld")\] public SoapEnvelope SendRequestResponse(SoapEnvelope envelope) { return base.SendRequestResponse("HelloWorld",envelope); }
}
My code is something like that: ------------------------------------------------------------------ Dim env As SoapEnvelope = New SoapEnvelope env.Context.Addressing.Action = New Action("http://analec.com/TestWebService/MyService/HelloWorld") env.CreateBody() env.Body.InnerXml = String.Format("") Dim epr As EndpointReference = New EndpointReference(New Uri("soap.tcp://localhost/TestWebService:80/MyService.asmx")) Dim client As MyHttpClient = New MyHttpClient(epr) Dim rs As SoapEnvelope = client.HelloWorld(env) Dim bodyResponse As String = rs.Body.OuterXml ------------------------------------------------------------------ ------------------------------------------------------------------ Option Explicit On Option Strict On Imports System.Xml Imports Microsoft.Web.Services2 Imports Microsoft.Web.Services2.Addressing Imports Microsoft.Web.Services2.Messaging Imports System.Web Public Class MyHttpClient Inherits SoapClient Public msg As String = String.Empty Public LoadTesterForm As LoadTesterForm = Nothing Public Sub New(ByVal dest As EndpointReference) MyBase.New(dest) End Sub _ Public Function HelloWorld(ByVal envelope As SoapEnvelope) As SoapEnvelope Dim response As SoapEnvelope response = MyBase.SendRequestResponse("HelloWorld", envelope) msg = response.Body.OuterXml LoadTesterForm.TestReport.Text = msg HelloWorld = response End Function Protected Overrides Sub FilterMessage(ByVal envelope As Microsoft.Web.Services2.SoapEnvelope) End Sub End Class ------------------------------------------------------------------ After executing this code i am getting an error message: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ERROR MESSAGE: An unhandled exception of type 'Microsoft.Web.Services2.AsynchronousOperationException' occurred in microsoft.web.services2.dll Additional information: WSE101: An asynchronous operation raised an exception. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I think this something related to authentication. Do you have any idea? Thanks Sumit Domyan
-
My code is something like that: ------------------------------------------------------------------ Dim env As SoapEnvelope = New SoapEnvelope env.Context.Addressing.Action = New Action("http://analec.com/TestWebService/MyService/HelloWorld") env.CreateBody() env.Body.InnerXml = String.Format("") Dim epr As EndpointReference = New EndpointReference(New Uri("soap.tcp://localhost/TestWebService:80/MyService.asmx")) Dim client As MyHttpClient = New MyHttpClient(epr) Dim rs As SoapEnvelope = client.HelloWorld(env) Dim bodyResponse As String = rs.Body.OuterXml ------------------------------------------------------------------ ------------------------------------------------------------------ Option Explicit On Option Strict On Imports System.Xml Imports Microsoft.Web.Services2 Imports Microsoft.Web.Services2.Addressing Imports Microsoft.Web.Services2.Messaging Imports System.Web Public Class MyHttpClient Inherits SoapClient Public msg As String = String.Empty Public LoadTesterForm As LoadTesterForm = Nothing Public Sub New(ByVal dest As EndpointReference) MyBase.New(dest) End Sub _ Public Function HelloWorld(ByVal envelope As SoapEnvelope) As SoapEnvelope Dim response As SoapEnvelope response = MyBase.SendRequestResponse("HelloWorld", envelope) msg = response.Body.OuterXml LoadTesterForm.TestReport.Text = msg HelloWorld = response End Function Protected Overrides Sub FilterMessage(ByVal envelope As Microsoft.Web.Services2.SoapEnvelope) End Sub End Class ------------------------------------------------------------------ After executing this code i am getting an error message: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ERROR MESSAGE: An unhandled exception of type 'Microsoft.Web.Services2.AsynchronousOperationException' occurred in microsoft.web.services2.dll Additional information: WSE101: An asynchronous operation raised an exception. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I think this something related to authentication. Do you have any idea? Thanks Sumit Domyan
In the MyHttpClient class, you override the FilterMessage method, but you didn't call this method of the base class:
Public Class MyHttpClient
Inherits SoapClient
...
Protected Overrides Sub FilterMessage(ByVal envelope As Microsoft.Web.Services2.SoapEnvelope)
MyBase.FilterMessage(envelope)
End Sub
End Class -
In the MyHttpClient class, you override the FilterMessage method, but you didn't call this method of the base class:
Public Class MyHttpClient
Inherits SoapClient
...
Protected Overrides Sub FilterMessage(ByVal envelope As Microsoft.Web.Services2.SoapEnvelope)
MyBase.FilterMessage(envelope)
End Sub
End ClassBut still i am getting the same error. Sumit Domyan
-
In the MyHttpClient class, you override the FilterMessage method, but you didn't call this method of the base class:
Public Class MyHttpClient
Inherits SoapClient
...
Protected Overrides Sub FilterMessage(ByVal envelope As Microsoft.Web.Services2.SoapEnvelope)
MyBase.FilterMessage(envelope)
End Sub
End ClassMy code is working fine now. Now i want to pass parameter in my HelloWorld web method. How can we pass parameters using SOAPEnvelope. Thanks for your kind help. Sumit Domyan
-
My code is working fine now. Now i want to pass parameter in my HelloWorld web method. How can we pass parameters using SOAPEnvelope. Thanks for your kind help. Sumit Domyan
You forgot the great article of Aaron Skonnard provided in my first post. You can provide the method parameters in the body of the message. Assume that I got a web method:
[WebMethod]
public string HelloWorld(string firstName, string lastName)
{
return "Hello " + firstName + " " + lastName;
}Now, you can build the body of the message like this:
...
string firstName = "Cristian";
string lastName = "Ronaldo";
env.Body.InnerXml = String.Format("<HelloWorld xmlns=\"http://tempuri.org/\\" ><firstName>{0}</firstName>" +
"<lastName>{1}</lastName></HelloWorld>", firstName, lastName);
... -
You forgot the great article of Aaron Skonnard provided in my first post. You can provide the method parameters in the body of the message. Assume that I got a web method:
[WebMethod]
public string HelloWorld(string firstName, string lastName)
{
return "Hello " + firstName + " " + lastName;
}Now, you can build the body of the message like this:
...
string firstName = "Cristian";
string lastName = "Ronaldo";
env.Body.InnerXml = String.Format("<HelloWorld xmlns=\"http://tempuri.org/\\" ><firstName>{0}</firstName>" +
"<lastName>{1}</lastName></HelloWorld>", firstName, lastName);
...Is this the only solution for getting response back. As i told you earlier, I want to call a web method of a web service, but I don’t want to use wsdl nor can I generate the proxy class. The scenario is that, in my application user can fill in the web service address and the method he wants to execute and I have to get the results. Here we are building the full XML for a method, which i cannot do. Because i dont know what parameters a methods takes. User will give us following information:
WebService URI Method name with parameters: HelloWorld("Cristian","Ronaldo")
No other information, we can ask from user. Thanks a lot Sumit Domyan