标准webservice调用

Posted jonney

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了标准webservice调用相关的知识,希望对你有一定的参考价值。

现代定义的webservice一般都倾向于restfull风格的http请求,但工作中还是会遇到前辈们写的时代代码。

我们更倾向于封装代码来调用,而不是服务引用。请看:

Service.asmx服务的注解

1     [WebService(Namespace = "http://shanghai.org/")]
2     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
3     [System.ComponentModel.ToolboxItem(false)]
4     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
5     // [System.Web.Script.Services.ScriptService]
6     public class Service : System.Web.Services.WebService
7     {}

Web.config配置文件重要节点

 1   <system.web>
 2     <httpRuntime requestValidationType="Service.RequestValidatorDisabled" />
 3     <webServices>
 4       <protocols>
 5         <add name="HttpPost"/>
 6         <add name="HttpGet"/>
 7       </protocols>
 8     </webServices>
 9     <compilation debug="true" targetFramework="4.0"/>
10   </system.web>
11 
12   <system.webServer>
13     <defaultDocument>
14       <files>
15         <add value="Service.asmx"/>
16       </files>
17     </defaultDocument>
18   </system.webServer>
1     public class RequestValidatorDisabled : System.Web.Util.RequestValidator
2     {
3         protected override bool IsValidRequestString(System.Web.HttpContext context, string value, System.Web.Util.RequestValidationSource requestValidationSource, string collectionKey, out int validationFailureIndex)
4         {
5 
6             validationFailureIndex = -1;
7             return true;
8         }
9     }

以上不加验证也可。

客户端调用工具

        public static string Serialize<T>(T entity, bool base64 = true)
        {
            using (System.IO.StringWriter sw = new System.IO.StringWriter())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                serializer.Serialize(sw, entity);

                if (base64)
                {
                    return Convert.ToBase64String(Encoding.UTF8.GetBytes(sw.ToString()));
                }
                else
                {
                    return sw.ToString();
                }
            }
        }

        public static T Deserialize<T>(string data, bool base64 = true)
        {
            var xml = "";
            if (base64)
            {
                xml = Encoding.UTF8.GetString(Convert.FromBase64String(data.Replace(" ", "+")));
            }
            else
            {
                xml = data;
            }

            using (System.IO.StringReader sr = new System.IO.StringReader(xml))
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(T));
                return (T)deserializer.Deserialize(sr);
            }
        }

请求方法(去掉服务器返回的服务的命名空间),都以POST方式提交

 1         public static T Post<T>(string url, Dictionary<string, string> kvs)
 2         {
 3             var data = "";
 4             foreach (var k in kvs.Keys)
 5             {
 6                 if (string.IsNullOrEmpty(data))
 7                 {
 8                     data += k + "=" + kvs[k];
 9                 }
10                 else
11                 {
12                     data += "&" + k + "=" + kvs[k];
13                 }
14             }
15 
16             HttpWebRequest rq = null;
17 
18             try
19             {
20                 rq = WebRequest.Create(url) as HttpWebRequest;
21             }
22             catch (Exception) { throw new Exception("错误:WEBSERVICE地址无效"); }
23 
24             rq.Method = "POST";
25             rq.ContentType = "application/x-www-form-urlencoded";
26 
27             byte[] bs = Encoding.UTF8.GetBytes(data);
28             rq.ContentLength = bs.Length;
29             try
30             {
31                 using (Stream writer = rq.GetRequestStream())
32                 {
33                     if (bs.Length > 0)
34                     {
35                         writer.Write(bs, 0, bs.Length);
36                     }
37                 }
38             }
39             catch (Exception) { throw new Exception("错误:发送失败,网络异常"); }
40 
41             var response = "";
42             try
43             {
44                 var rp = rq.GetResponse() as HttpWebResponse;
45 
46                 using (Stream reader = rp.GetResponseStream())
47                 {
48                     response = new StreamReader(reader, Encoding.UTF8).ReadToEnd();
49                 }
50             }
51             catch (Exception err) { throw new Exception("错误:接收失败,网络异常"); }
52 
53             try
54             {
55                 return Deserialize<T>(TrimXmlns(response), false);
56             }
57             catch (Exception err) { throw new Exception("错误:数据序列化异常"); }
58         }
59 
60         private static string TrimXmlns(string source)
61         {
62             return source.Replace("xmlns=\"http://shanghai.org/\"", "");
63         }

服务端定义接口

 1         [WebMethod(Description = "导入Excel移库计划")]
 2         public TransExcelDto ImportTransExcel(string data)
 3         {
 4             try
 5             {
 6                 var tf = Common.Utilities.Deserialize<TransExcelDto>(data);
 7 
 8                 return DataAccess.ImportTransExcel(tf);
 9             }
10             catch (Exception e)
11             {
12                 return new TransExcelDto().Failure(98, e.Message) as TransExcelDto;
13             }
14         }

调用举例,注意下面的key = data,即使接口的参数

1 TransExcelDto request = new TransExcelDto() { SessionId = Cache.SessionId };
2 3 
4 var postData = new Dictionary<string, string>();
5 postData.Add("data", Common.Utilities.Serialize<TransExcelDto>(request));
6 
7 TransExcelDto result = Common.Utilities.Post<TransExcelDto>(Metadata.WebServiceUrl + "/ImportTransExcel", postData);

 

以上是关于标准webservice调用的主要内容,如果未能解决你的问题,请参考以下文章

webService—调用webService

C#动态调用webService出现 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。

WebService服务介绍与调用

springboot 整合 cxf 发布webservice与调用

如何利用django实现webservice接口文件上传的功能

java webservice