Link to home
Create AccountLog in
Avatar of Jzzzz
JzzzzFlag for United States of America

asked on

Dynamically invoke a web service method wiith (URL with IP, Service Name, Port, Namespace)

Hi,

I am trying to Dynamically invoke a web service. The code was originally in Java and we are migratiing to .NET.

To invoke a webservice, we usually generate web referece based on wsdl generated code and use it.

Instead, if i have to dynamically invoke a web service method, given that i know the following

- URL with ip
- Service name
- port
- namespace

This is available in java, sample code given below. I need to implement the same in c#. Any pointers will be a BIG BIG HELP for me .

Java has some 3rd party objects as shown in the code below.


//URL wsdl = new URL(url);
String tempServiceName = getServiceName(serviceName);
QName serviceQName = new QName(targetNameSpace, tempServiceName);
QName port = new QName(targetNameSpace, portName);
QName operation;
operation= new QName(targetNameSpace, reqInfo.getRequestMethod());

org.apache.axis.client.ServiceFactory factory = new ServiceFactory();
//org.apache.axis.client.Service service = (Service) factory.createService(new URL(url), serviceQName);
org.apache.axis.client.Service service = (Service) factory.createService(serviceQName);
org.apache.axis.client.Call call = (org.apache.axis.client.Call) service.createCall(port, operation);
call.setReturnType(XMLType.XSD_STRING);
call.addParameter("string", XMLType.XSD_STRING, ParameterMode.IN);

//-------------settign the header
org.apache.axis.message.PrefixedQName svcName = new org.apache.axis.message.PrefixedQName("http://schemas.xmlsoap.org/ws/2002/07/secext", "Security", "wsse");
org.apache.axis.message.SOAPHeaderElement sh = new org.apache.axis.message.SOAPHeaderElement(svcName);
javax.xml.soap.SOAPElement sub = sh.addChildElement("UsernameToken");
javax.xml.soap.SOAPElement element = sub.addChildElement("Username");
element.addTextNode(userName);
element = sub.addChildElement("Password");
element.addTextNode(password);
call.addHeader(sh);
//------------

// int iIndexWsdl = url.indexOf("?");
// String strEndPointAddress = url.substring(0, iIndexWsdl);
call.setTargetEndpointAddress(url);
// String requestXML= CommonHelper.getInstance().loadFile(reqInfo.getRequestXML().trim()).toString();
logger.info("request XML:"+reqInfo.getRequestXML());
//long startTime=logger.perf("calling webservice");
long startTime= System.currentTimeMillis();
String response = (String) call.invoke(new Object[]{reqInfo.getRequestXML().trim()});
// long endTime=logger.perf("timeTaken for webserviceCall:",startTime);
long endTime=System.currentTimeMillis();
Avatar of nsanga
nsanga

You can use this......but you have to do some modifications according to your requirement....

Add the port as part of url like http://localhost:8080/mywebservice


 Public Function SubmitToServer(ByVal pdomQuery As Xml.XmlDocument) As Xml.XmlNode

        Dim lobjCookie As System.Net.CookieContainer = Nothing
        Dim lreqRequest As System.Net.HttpWebRequest
        Dim lresResponse As System.Net.HttpWebResponse
        Dim ldomReply As New Xml.XmlDocument

        If lobjCookie Is Nothing Then lobjCookie = New System.Net.CookieContainer

        lreqRequest = CType(System.Net.HttpWebRequest.Create(mobjServerURL), System.Net.HttpWebRequest)
        lreqRequest.CookieContainer = lobjCookie
        lreqRequest.Credentials = System.Net.CredentialCache.DefaultCredentials
        lreqRequest.Method = "POST"

        lreqRequest.Timeout = 600000

        lreqRequest.ContentType = "text/xml"
        lreqRequest.Accept = "text/xml"

        Dim lstrmStream As System.IO.Stream = lreqRequest.GetRequestStream()
        pdomQuery.Save(lstrmStream)
        lstrmStream.Close()

        lresResponse = CType(lreqRequest.GetResponse(), System.Net.HttpWebResponse)

        ' Stream in the Response
        Dim lobjResponse As New IO.StreamReader(lresResponse.GetResponseStream())
        Dim lstrResponse As String = lobjResponse.ReadToEnd
        lobjResponse.Close()
        lresResponse.Close()

        If lstrResponse = "" Then
            Dim lobjNewException As New Exception
            Throw lobjNewException
        End If

        Try
            ldomReply.LoadXml(lstrResponse)
        Catch lobjException As Exception
            Dim lobjNewException As New Exception
            Throw lobjNewException
        End Try

        Return ldomReply.SelectSingleNode("/")

    End Function
where can be created as

mobjServerURL = New Uri(pstrServerURL)
sorry for typo....


where mobjServerURL  can be created as

mobjServerURL = New Uri(pstrServerURL)
Avatar of Jzzzz

ASKER

Nsanga , your soultion is a simple invoke. I need to dynamically create proxy add SOAP header  and operation and namespace  as in the Java code I have above in question.
Avatar of Jzzzz

ASKER

And mainly I dont know the wsdl. i.e wsdl is not exposed by the webserviceHost. I have only URL, PORT, Servicename and Operation.
ASKER CERTIFIED SOLUTION
Avatar of Jzzzz
Jzzzz
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Jzzzz

ASKER

Found my Solution.
Closed, 500 points refunded.
Netminder
Site Admin