首页 新闻 赞助 找找看

.net定制webservice接口进行数据对接

0
悬赏园豆:20 [已解决问题] 解决于 2018-03-22 10:58

有没有这一类的参考文档或者接口实例?(本人首次开发接口,对这流程操作都不是很了解,希望给个意见)

ShareMyself的主页 ShareMyself | 初学一级 | 园豆:12
提问于:2018-03-15 13:14
< >
分享
最佳答案
0

最简单的,如果存在mex,那么vs直接引用,当普通类使用。

再给你个http封装soap的方法,其他的给你讲也讲不撑钭~

 

public virtual void LoadData()
{
if (LoadingForm.IsDownloading) return;
if (!ValidateDealling(Validate())) return;
LoadingForm.Show();
var webClient = new WebClient();
webClient.UploadStringCompleted += (sender, args) =>
{
if (!LoadingForm.IsDownloading) return;
if (args.Error != null)
{
if (OnError != null) OnError.Invoke(new NetWorkException("网络错误", args.Error));
}
else if (args.Result == null)
{
if (OnError != null) OnError.Invoke(new NetWorkException("数据错误", args.Error));
}
else
{
try
{
XDocument doc = XDocument.Parse(args.Result);
Data = doc.Root.Value;
if (DownloadSucesss != null) DownloadSucesss.Invoke(Data);
}
catch (Exception ex)
{
OnError.Invoke(new NetWorkException("数据读取错误", ex));
}
}
LoadingForm.Close();
};
webClient.Headers["SOAPAction"] = string.Format("http://tempuri.org/{0}", Fun);
webClient.Headers["Content-Type"] = "text/xml; charset=utf-8";
string soapBody = CreateBody();
webClient.UploadStringAsync(
new Uri(string.Format("http://{0}:8001/{1}", WebServiceHelper.Host, Uri), UriKind.Absolute), soapBody);
}


private string CreateBody()
{
var contentBody = new StringBuilder();
contentBody.Append("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
contentBody.Append(
"<s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");

contentBody.AppendFormat("<{0} xmlns=\"http://tempuri.org/\">", Fun);
foreach (IParamQuery funParam in ParamsQuery)
contentBody.AppendFormat("<{0}>{1}</{0}>", funParam.PName, funParam.PValue);
contentBody.AppendFormat("</{0}>", Fun);

contentBody.Append("</s:Body>");
contentBody.Append("</s:Envelope>");
return contentBody.ToString();
}

收获园豆:20
花飘水流兮 | 专家六级 |园豆:13560 | 2018-03-15 21:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册