后台通过soap 1.1 post 调用 webservice

定义 soap 1.1 post 调用webservice 

public static XmlDocument Test3_QuerySoapWebService(String URL,
        String MethodName,
        Hashtable Pars)
    {
        #region 获取命名空间
        HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(URL + "?WSDL");
        request1.Credentials = CredentialCache.DefaultCredentials;
        request1.Timeout = 10000;
        WebResponse response = request1.GetResponse();
        StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
        XmlDocument doc1 = new XmlDocument();
        doc1.LoadXml(sr.ReadToEnd());
        sr.Close();
        var XmlNs = doc1.SelectSingleNode("//@targetNamespace").Value;
        #endregion

        #region  请求信息
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
        request.Method = "POST";
        request.ContentType = "text/xml; charset=utf-8 ";
        request.Headers.Add("SOAPAction", XmlNs + MethodName);
        request.Credentials = CredentialCache.DefaultCredentials;
        request.Timeout = 10000;
        #endregion

        #region 要传入的参数,是xml文档对象(http post,http get 是字符串)
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(
            "<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:Envelope>"
          );
        XmlDeclaration dec2 = doc.CreateXmlDeclaration("1.0", "utf-8", null);
        doc.InsertBefore(dec2, doc.DocumentElement);

        XmlElement soapBody = doc.CreateElement("soap",
            "Body", "http://schemas.xmlsoap.org/soap/envelope/");
        XmlElement soapMethod = doc.CreateElement(MethodName);
        soapMethod.SetAttribute("xmlns", XmlNs);
        foreach (string k in Pars.Keys)
        {
            XmlElement soapPar = doc.CreateElement(k);
            soapPar.InnerXml = ObjectToSoapXml(Pars[k]);
            soapMethod.AppendChild(soapPar);
        }
        soapBody.AppendChild(soapMethod);
        doc.DocumentElement.AppendChild(soapBody);
        byte[] data = Encoding.UTF8.GetBytes(doc.OuterXml);
        #endregion

        #region 把参数写入请求流中
        request.ContentLength = data.Length;
        Stream writer = request.GetRequestStream();
        writer.Write(data, 0, data.Length);
        writer.Close();
        #endregion

        #region 读取响应内容       
        try
        {
            response = (HttpWebResponse)request.GetResponse();
        }
        catch (WebException ex)
        {
            response = (HttpWebResponse)ex.Response;
            var sr1 = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            var strHtml = sr1.ReadToEnd();
        }

        XmlDocument doca = ReadXmlResponse(response);
        return doca;
        //XmlNamespaceManager mgr = new XmlNamespaceManager(doca.NameTable);
        //mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
        //String RetXml = doca.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;

        //XmlDocument docb = new XmlDocument();
        //docb.LoadXml("<root>" + RetXml + "</root>");
        //XmlDeclaration decl = docb.CreateXmlDeclaration("1.0", "utf-8", null);
        //docb.InsertBefore(decl, docb.DocumentElement);
        //return docb;
        #endregion
    }

 

调用示例: 

  protected void Page_Load(object sender, EventArgs e)
        {
            Hashtable pars = new Hashtable();
            String Url = "http://localhost:63596/WebService1.asmx";
            pars["aa"] = "HelenZhou";
            XmlDocument doc = WebSvcCaller.
                Test3_QuerySoapWebService(Url,
                "MyHelloWorld",
                pars);
            Response.Write(doc.OuterXml); 
        }

 

转载:http://www.cnblogs.com/alvin2013/articles/5220892.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值