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.Ser
viceFactor
y factory = new ServiceFactory();
//org.apache.axis.client.S
ervice service = (Service) factory.createService(new URL(url), serviceQName);
org.apache.axis.client.Ser
vice service = (Service) factory.createService(serv
iceQName);
org.apache.axis.client.Cal
l call = (org.apache.axis.client.Ca
ll) service.createCall(port, operation);
call.setReturnType(XMLType
.XSD_STRIN
G);
call.addParameter("string"
, XMLType.XSD_STRING, ParameterMode.IN);
//-------------settign the header
org.apache.axis.message.Pr
efixedQNam
e svcName = new org.apache.axis.message.Pr
efixedQNam
e("
http://schemas.xmlsoap.org/ws/2002/07/secext", "Security", "wsse");
org.apache.axis.message.SO
APHeaderEl
ement sh = new org.apache.axis.message.SO
APHeaderEl
ement(svcN
ame);
javax.xml.soap.SOAPElement
sub = sh.addChildElement("Userna
meToken");
javax.xml.soap.SOAPElement
element = sub.addChildElement("Usern
ame");
element.addTextNode(userNa
me);
element = sub.addChildElement("Passw
ord");
element.addTextNode(passwo
rd);
call.addHeader(sh);
//------------
// int iIndexWsdl = url.indexOf("?");
// String strEndPointAddress = url.substring(0, iIndexWsdl);
call.setTargetEndpointAddr
ess(url);
// String requestXML= CommonHelper.getInstance()
.loadFile(
reqInfo.ge
tRequestXM
L().trim()
).toString
();
logger.info("request XML:"+reqInfo.getRequestXM
L());
//long startTime=logger.perf("cal
ling webservice");
long startTime= System.currentTimeMillis()
;
String response = (String) call.invoke(new Object[]{reqInfo.getReques
tXML().tri
m()});
// long endTime=logger.perf("timeT
aken for webserviceCall:",startTime
);
long endTime=System.currentTime
Millis();
Start Free Trial