c# - Pass header parameters to dynamically invoked webservice -
i trying dynamically invoce webservice required header authentication can't figure out how should pass required header credentials
my code:
public object[] generatewebserviceproxy(string wsdl) { webrequest webrequest = webrequest.create(wsdl); system.io.stream requeststream = webrequest.getresponse().getresponsestream(); servicedescription description = servicedescription.read(requeststream); string sdname = description.services[0].name; string method_name = description.messages["loginsoapin"].name.replace("request", ""); method_name = method_name.replace("soapin", ""); servicedescriptionimporter importer = new servicedescriptionimporter(); importer.protocolname = "soap"; importer.addservicedescription(description, null, null); codenamespace nmspace = new codenamespace(); codecompileunit unit1 = new codecompileunit(); unit1.namespaces.add(nmspace); servicedescriptionimportwarnings warning = importer.import(nmspace, unit1); object[] objret = null; if (warning == 0) { codedomprovider provider1 = new csharpcodeprovider(); string[] assemblyreferences = new string[3]{ "system.web.services.dll", "system.xml.dll", "system.data.dll" }; compilerparameters parms = new compilerparameters(assemblyreferences); compilerresults results = codedomprovider.createprovider("csharp").compileassemblyfromdom(parms, unit1); object o = results.compiledassembly.createinstance(sdname); type t = o.gettype(); //propertyinfo credentials = t.getproperty("credentials"); //propertyinfo credentials = t.getproperty("credentials"); // credentials.setvalue(o, new networkcredential(username, passwd), null); methodinfo m = t.getmethod("invoke", bindingflags.nonpublic | bindingflags.instance); object[] webmethod_param = { "username", "password" }; object[] objparams = { method_name, webmethod_param }; objret = (object[])m.invoke(o, objparams); if (objret.length > 0) { objret = (object[])objret[0]; } } homecoming objret; }
the request should following:
tem:authheader> <tem:username>username</tem:username> <tem:password>password</tem:password> </tem:authheader
ive tried both ways pass credentials (as can se in code) nil seems work.
you've retrieve clientcredentials property clientbase object. (the "o" in code) , set username , password :
example :
clientcredentials.username.username = "username"; clientcredentials.username.password = "password";
from msdn : http://msdn.microsoft.com/it-it/library/ms599072.aspx
the clientcredentials accessed through clientcredentials property of clientbase class.
c# web-services
No comments:
Post a Comment