c# 纯代码调用 webservice

public static class RequestHelper
{
public class RequestResult
{
public RequestResult(bool requestResult, string resultData)
{
this.Result = requestResult;
this.ResultData = resultData;
} public bool Result { get; private set; }
public string ResultData { get; private set; }
} public static RequestResult Request(string url, string method, string data, Dictionary<string, string> header, string contentType = "application/json;charset=utf-8")
{
HttpWebRequest request = null;
if (url.ToLower().StartsWith("https"))
{
ServicePointManager.ServerCertificateValidationCallback =
new System.Net.Security.RemoteCertificateValidationCallback(delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
});
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
request = WebRequest.Create(url) as HttpWebRequest; request.Method = method;
request.AllowAutoRedirect = true;
request.Proxy = new WebProxy { UseDefaultCredentials = true };
request.ContentType = contentType;
foreach (var h in header)
request.Headers.Add(h.Key, h.Value); try
{
if (data != null && method != "GET")
{
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(data);
}
} var response = request.GetResponse() as HttpWebResponse;
var responseStream = response.GetResponseStream();
var reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
string result = reader.ReadToEnd();
return new RequestResult(true, result);
}
catch (Exception ex)
{
return new RequestResult(false, ex.Message);
}
}
}
var serviceUrl = @"http://192.168.0.xxx:xxxx/webService/xxxx.asmx";
var header = new Dictionary<string, string>();
header.Add("SOAPAction", "http://tempuri.org/GetAuthorizationCode");
var body =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ " <soap:Body>"
+ " <GetAuthorizationCode xmlns=\"http://tempuri.org/\">"
+ " <loginName>180001</loginName>"
+ " <token>6B17644986B630E9C284C07F4D412C89</token>"
+ " </GetAuthorizationCode>"
+ " </soap:Body>"
+ "</soap:Envelope>"; var result = RequestHelper.Request(serviceUrl, "POST", body, header, "text/xml;charset=utf-8");
if (!result.Result)
return; var doc = new XmlDocument();
var namespaceManager = new XmlNamespaceManager(doc.NameTable);
namespaceManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
namespaceManager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
namespaceManager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
namespaceManager.AddNamespace("mynamespace", "http://tempuri.org/");
doc.LoadXml(result.ResultData);
var resultNode = doc.SelectSingleNode(@"soap:Envelope/soap:Body/mynamespace:GetAuthorizationCodeResponse/mynamespace:GetAuthorizationCodeResult", namespaceManager);
var resultFlag = bool.Parse(resultNode.InnerText);

其中调用和解析的xml样式,请参照接口中格式中的内容。

上一篇:DDL/DML/DCL


下一篇:Jena TDB Assembler