
关于C#调用WebServices的方法
2018-1-22
前情是我使用vs在引用高通的webservice时出现了下载错误导致无法引用这个服务,先是在网上查询了这个错误的问题及解决方案,将这个问题与解决方法发给了高通同事,可惜的是他也不清楚怎么回事,没有办法我只好再去查阅资料看看其他方法;这里总结几种方法
第一种:最常见的就是直接在项目的引用中右击添加服务引用

此方法用法如下:
首先需要添加引用;然后需要实例化,实例化完了之后就可以通过实例化的对象调用webservices的方法;
这个方法以前常用,可惜这次不行了;
第二种:不用通过引用,直接调用webservices
此方法又分为了几种:
(1)使用HttpWebRequest 向WebService发送POST请求,并将请求头:ContentType = "application/json;charset=utf-8",参数以JSON方式发送给WebService
调用方法如下:
Hashtable ht = new Hashtable();
ht.Add("LoginName", "Admin");
ht.Add("Password", "Password");
ht.Add("Aey", "123");
HttpHelper.PostWebServiceByJson("http://localhost/OpenApi/MobileService.asmx", "Login", ht);
注意以上调用WebService方法,需要在WebService中将以下代码中的[System.Web.Script.Services.ScriptService]注释去掉
(2)根据WebService文档对应方法说明(例如参数提交方式,请求)发送POST或Get请求

以下是 SOAP 1.2 请求和响应示例。动态方法调用 为什么不安全所显示的占位符需替换为实际值。
POST /OpenApi/MobileService.asmx HTTP/1.1
Host: gps.szwearable.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Login"
<?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>
<Login xmlns="http://tempuri.org/">
<LoginName>string</LoginName>
<Password>string</Password>
<Aey>string</Aey>
</Login>
</soap:Body>
</soap:Envelope>
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-65451-1.html
02不是太滑