C〓〓SOAPを使用してwebserviceの実例解析を取得する

5610 ワード

本論文は主に以下の2つのリンクを参照し、整理する。JavaはSOAPを使用する。http://www.cnblogs.com/linjiqin/archive/2012/05/07/2488880.html C〓send soap and get reponse:http://stackoverflow.com/questions/4791794/client-to-send-soap-request-and-received-response
1.webservice提供者:http://www.webxml.com.cn/zh_cn/index.aspx 2.以下は「テンセントQQオンライン状態を取得する」という例を示します。ホームページ紹介の参考:http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?op=qqCheckOnline
コードは以下の通りです
using System.IO;
using System.Xml;
using System.Net;

namespace ConsoleApplicationTest2
{
    class SOAPTest
    {
        public static void CallWebService(string qq)
        {
            var _url = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx";
            var _action = "http://WebXml.com.cn/qqCheckOnline";

            XmlDocument soapEnvelopeXml = CreateSoapEnvelope(qq);
            HttpWebRequest webRequest = CreateWebRequest(_url, _action);
            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

            // begin async call to web request.
            IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

            // suspend this thread until call is complete. You might want to
            // do something usefull here like update your UI.
            asyncResult.AsyncWaitHandle.WaitOne();

            // get the response from the completed web request.
            string soapResult;
            using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
            {
                using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                {
                    soapResult = rd.ReadToEnd();
                }
                Console.WriteLine(soapResult);
            }
        }

        private static HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }

        private static XmlDocument CreateSoapEnvelope(string qq)
        {
            XmlDocument soapEnvelop = new XmlDocument();
            string soapXml = @"qq_Code";
            soapEnvelop.LoadXml(soapXml.Replace("qq_Code",qq));
            return soapEnvelop;
        }

        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }

        static void test()
        {
            string[] qq = { "49", "4941", "4949252", "494925223", "4949252242", "48492522502", "49492522" };
            foreach (var qc in qq)
                SOAPTest.CallWebService(qc);
            Console.ReadKey();
        }
    }
}
次の図のように実行結果が得られます。C#使用SOAP获取webservice实例解析_第1张图片