调用WebService接口几种常用方式

  • 11K 浏览
  • 最后发表 2018-11-03 03:27
huangyh 发布于 2018-11-03 03:27

准备工作

百度随机找个可以测试的WebService测试地址

例如:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx

getSupportCityString 

    获得支持的城市/地区名称和与之对应的ID

输入参数:theRegionCode = 省市、国家ID或名称,返回数据:一维字符串数组。

 

方案一

简单的调用:即通过鼠标手动添加Web服务引用的方式(课本知识不多介绍)。

 

方案二

方便的调用:代理类

生成:

在Visual Studio 命令提示窗口去执行命令

wsdl http://ws.webxml.com.cn/WebServices/WeatherWS.asmx /o:d:/ WeatherWS.cs /n: WebServiceTest

/o: 生成路径

/n: 生成类的命名空间

调用:

WeatherWS service = new WeatherWS();
service.getSupportCityString("上海");

 若需要设置代理服务器或者身份验证,手动修改生成的代理类,添加代码如下:

//代理设置
this.Proxy = new WebProxy("代理地址", true, null, new NetworkCredential("代理的账号", "代理的密码"));
//身份验证
this.Credentials = new NetworkCredential("身份验证的账号", "身份验证的密码");

 

方案三

通用的调用:HttpWebRequest 类

GET 方式通过在网络地址附加参数来完成数据的提交

var url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString?theRegionCode=上海";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    string result = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")).ReadToEnd();
    //在这里对接收到的页面内容进行处理 
}

POST 方式通过在页面内容中填写参数的方法来完成数据的提交

var url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString";
string param = "theRegionCode=上海"; //参数
byte[] bs = Encoding.UTF8.GetBytes(param);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bs.Length;
using (Stream reqStream = request.GetRequestStream())
{
    reqStream.Write(bs, 0, bs.Length);
}
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    string result = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")).ReadToEnd();
    //在这里对接收到的页面内容进行处理 
}

 

方案四

推荐一个.NET的HTTP辅助类,RestSharp:一个轻量的,不依赖任何第三方的组件或者类库的Http的组件

RestSharp具有以下的优点:(该优点为百度复制过来的)

  1. 支持.NET 3.5+,Silverlight 4, Windows Phone 7, Mono, MonoTouch, Mono for Android, Compact Framework 3.5等
  2. 通过NuGet方便引入到任何项目 ( Install-Package restsharp )
  3. 可以自动反序列化XML和JSON
  4. 支持自定义的序列化与反序列化
  5. 自动检测返回的内容类型
  6. 支持HTTP的GET, POST, PUT, HEAD, OPTIONS, DELETE等操作
  7. 可以上传多文件
  8. 支持oAuth 1, oAuth 2, Basic, NTLM and Parameter-based Authenticators等授权验证等
  9. 支持异步操作
  10. 极易上手并应用到任何项目中

GET请求

var client = new RestClient("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString?theRegionCode=上海");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var result = response.Content;
//在这里对接收到的页面内容进行处理

POST请求

var client = new RestClient("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx");
var request = new RestRequest(Method.GET);

var xml = new StringBuilder();
xml.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
xml.AppendLine("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">");
xml.AppendLine("<soap12:Body>");
xml.AppendLine("<getSupportCityString xmlns=\"http://WebXml.com.cn/\">");
xml.AppendLine("<theRegionCode>");
xml.AppendLine("上海");
xml.AppendLine("</theRegionCode>");
xml.AppendLine("</getSupportCityString>");
xml.AppendLine("</soap12:Body>");
xml.AppendLine("</soap12:Envelope>");
request.RequestFormat = RestSharp.DataFormat.Xml;

request.AddParameter("application/soap+xml; charset=utf-8", xml, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var result = response.Content;
//在这里对接收到的页面内容进行处理

 

强烈推荐必不可少的调试工具

1.Postman 

2.SoapUI

 

        作者留言:

       本文不提供任何下载地址,以防地址带毒危害群众。

       本文若有错误,敬请提出指正。

       本文版权归作者所有,欢迎转载,但必须保留原文链接。

  • 喜欢
  • steve.shi

要回复问题请先登录注册

Close