zoukankan      html  css  js  c++  java
  • C#调用WebService的简单方式

    WebServiceCallpublic class WebServiceCall
        {
            public void Call()
            {
                string url = "http://localhost:1117/WebSite/WebService.asmx";
                string data = GetSOAPReuquestBody("100");
               HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);            
                req.ContentType = "text/xml; charset=utf-8";
                req.Method = "POST";
                using (Stream reqStream = req.GetRequestStream())
                {
                    byte[] reqData = Encoding.UTF8.GetBytes(data);
                    reqStream.Write(reqData, 0, reqData.Length);
                }
                 
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Console.WriteLine(resp.StatusCode);
                foreach (var item in resp.Headers.AllKeys)
                {
                    Console.WriteLine(item + " : " + resp.Headers[item]);                
                }
                using (StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
                {
                    Console.WriteLine(reader.ReadToEnd());
                }
    
            }
            public void Call2()
            {
                string url = "http://localhost:1117/WebSite/WebService.asmx/GetNumber";
                string data = "id=3";
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "POST";
                using (Stream reqStream = req.GetRequestStream())
                {
                    byte[] reqData = Encoding.UTF8.GetBytes(data);
                    reqStream.Write(reqData, 0, reqData.Length);
                }
    
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Console.WriteLine(resp.StatusCode);
                foreach (var item in resp.Headers.AllKeys)
                {
                    Console.WriteLine(item + " : " + resp.Headers[item]);
                }
                using (StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
                {
                    Console.WriteLine(reader.ReadToEnd());
                }
    
            }
    
            public string GetSOAPReuquestBody(string param)
            {
                StringBuilder soap = new StringBuilder();
                soap.Append("<?xml version="1.0" encoding="utf-8"?>");
                soap.Append("<soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">");
                soap.Append("<soap12:Body>");
                soap.Append("<GetNumber  xmlns="http://tempuri.org/">");
                soap.Append("<id>");
                soap.Append(param);
                soap.Append("</id>");
                soap.Append("</GetNumber>");
                soap.Append("</soap12:Body>");
                soap.Append("</soap12:Envelope>");
                return soap.ToString();
            }
        }

    http://www.cnblogs.com/disappearwind/articles/2633760.html

  • 相关阅读:
    Linux 文件系统层次结构 笔记 day 02
    新买服务器 设置ssh秘钥登录
    FastSocket客户端/服务端通讯示例 客户端被动接收
    FastSocket学习笔记~制定自已的传输协议~续~制定基于FastSocket的协议
    Oracle 查看表空间使用率
    Oracle查看用户占用的表空间大小
    failed to flush export bulk [default_local]
    Elasticsearch7.x配置文件
    master not discovered yet, this node has not previously joined a bootstrapped (v7+) cluster
    4.4 rust Smart Pointers Box and Deref 及 空针指
  • 原文地址:https://www.cnblogs.com/ilookbo/p/5481780.html
Copyright © 2011-2022 走看看