1. 程式人生 > >後臺通過soap 1.1 post 呼叫 webservice

後臺通過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); 
        }