直接代碼POST數據調用WebService

ps:使用過webservice的童鞋大概都明白它是基於Soap協議交換數據的,同時Soap協議是對HTTP協議的擴展,其實咱們就能夠認爲調用一個WEB服務就是經過http協議GET或POST數據的過程,只不過中間的輸入/輸出數據是遵照Soap協議格式的標準XML。明白這個道理以後咱們就能夠經過構造請求數據來模擬調用WEB服務的過程了,具體代碼以下:web

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Encoding encoding = Encoding.UTF8;
            string responseData = String.Empty;            

            WebRequest request = HttpWebRequest.Create("http://localhost:32100/Service1.svc");
            string param =  "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\" xmlns:wcf=\"http://schemas.datacontract.org/2004/07/WcfService2\">"
                            +"   <soapenv:Header/>"
                            +"   <soapenv:Body>"
                            +"      <tem:GetDataUsingDataContract>"
                            +"         <!--Optional:-->"
                            +"         <tem:composite>"
                            +"            <!--Optional:-->"
                            +"            <wcf:BoolValue>true</wcf:BoolValue>"
                            +"            <!--Optional:-->"
                            +"            <wcf:StringValue>ddd</wcf:StringValue>"
                            +"         </tem:composite>"
                            +"      </tem:GetDataUsingDataContract>"
                            +"   </soapenv:Body>"
                            +"</soapenv:Envelope>";
            byte[] bs = System.Text.ASCIIEncoding.ASCII.GetBytes(param);

            request.Method = "POST";
            request.ContentType = "text/xml;charset=UTF-8";
            request.ContentLength = bs.Length;

            //指定要調用http://localhost:32100/Service1.svc服務中的GetDataUsingDataContract方法
            //http://tempuri.org/IService1/GetDataUsingDataContract(名稱空間(http://tempuri.org/)/接口名稱(IService1)/方法名(GetDataUsingDataContract))
            request.Headers["SOAPAction"] = "http://tempuri.org/IService1/GetDataUsingDataContract"; 


            using(Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
                reqStream.Close();
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                {
                    responseData = reader.ReadToEnd().ToString();
                }
                Console.Write(responseData);
                Console.ReadKey();
            }


        }
    }
}

 

固然,上面的例子徹底是對請求數據進行硬編碼,實際使用還得根據實際狀況調整和封裝。下圖是調用後的結果,就是一堆符合SOAP格式的XML,至於怎麼解析我就不說了。工具

 

這裏還有一點要提一下的是如何抓取WEBService的請求信息,我是經過SoapUI這個工具來獲取的。(沒使用過SoapUI???請教度娘吧。。。)編碼

 

編寫此文的目的:JAVA開發的WebService經過.NET來調用時常常會出現一些莫名其妙的問題(二者標準不同,不兼容)折騰好久得不到解決,因此只能出此下策了。spa

相關文章
相關標籤/搜索