c#动态调用Webservice的两种方法实例
程序员文章站
2023-12-20 17:12:16
方法一:复制代码 代码如下:hashtable ht = new hashtable();  ...
方法一:
复制代码 代码如下:
hashtable ht = new hashtable();
ht.add("a", "testhelloworld");
xmldocument xx = webserviceshelper.querysoapwebservice("//www.jb51.net/elab_mgmt/workflowschemetaskserivce.asmx", "atesting", ht);
string ss = xx.outerxml;
复制代码 代码如下:
/// <summary>
/// 通用webservice调用(soap),参数pars为string类型的参数名、参数值
/// </summary>
public static xmldocument querysoapwebservice(string url, string methodname, hashtable pars)
{
if (_xmlnamespaces.containskey(url))
{
return querysoapwebservice(url, methodname, pars, _xmlnamespaces[url].tostring());
}
else
{
return querysoapwebservice(url, methodname, pars, getnamespace(url));
}
}
复制代码 代码如下:
private static xmldocument querysoapwebservice(string url, string methodname, hashtable pars, string xmlns)
{
_xmlnamespaces[url] = xmlns;//加入缓存,提高效率
httpwebrequest request = (httpwebrequest)httpwebrequest.create(url);
request.method = "post";
request.contenttype = "text/xml; charset=utf-8";
request.headers.add("soapaction", "\"" + xmlns + (xmlns.endswith("/") ? "" : "/") + methodname + "\"");
setwebrequest(request);
byte[] data = encodeparstosoap(pars, xmlns, methodname);
writerequestdata(request, data);
xmldocument doc = new xmldocument(), doc2 = new xmldocument();
doc = readxmlresponse(request.getresponse());
xmlnamespacemanager mgr = new xmlnamespacemanager(doc.nametable);
mgr.addnamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
string retxml = doc.selectsinglenode("//soap:body/*/*", mgr).innerxml;
doc2.loadxml("<root>" + retxml + "</root>");
adddelaration(doc2);
return doc2;
}
复制代码 代码如下:
private static string getnamespace(string url)
{
httpwebrequest request = (httpwebrequest)webrequest.create(url + "?wsdl");
setwebrequest(request);
webresponse response = request.getresponse();
streamreader sr = new streamreader(response.getresponsestream(), encoding.utf8);
xmldocument doc = new xmldocument();
doc.loadxml(sr.readtoend());
sr.close();
return doc.selectsinglenode("//@targetnamespace").value;
}
方法二:
通过soapui直接取url
复制代码 代码如下:
string postdata2="<soapenv:envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\"><soapenv:header/><soapenv:body><tem:atesting><!--optional:--><tem:a>?</tem:a></tem:atesting></soapenv:body></soapenv:envelope>";
httphelper.getresponseformurlasync("http://www.xxx.com/testingservices.asmx?wsdl", postdata2, "text/xml", true, new asynccallback(responsecallback));
复制代码 代码如下:
}
static void responsecallback(iasyncresult ar)
{
httpwebrequest req = ar.asyncstate as httpwebrequest;
if (req == null)
return;
try
{
httpwebresponse response = req.endgetresponse(ar) as httpwebresponse;
if (response.statuscode != httpstatuscode.ok)
{
response.close();
loghelper.error("定时任务", "异步执行失败," + req.requesturi.tostring() + "\r\nresponse状态代码为\r\n" + response.statuscode.tostring());
return;
}
stream responsestream = response.getresponsestream();
streamreader reader = new streamreader(responsestream, encoding.utf8);
string responsestr = reader.readtoend();
responsestream.close();
loghelper.warn("定时任务", req.requesturi.tostring() + "\r\n" + responsestr);
}
catch (exception e)
{
loghelper.fatal("定时任务", req.requesturi.tostring() + "\r\n执行失败", e);
}
}