C#发起soap请求!!!好东西啊

来源:互联网 发布:jeesz源码在哪下载 编辑:程序博客网 时间:2024/06/06 07:38

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using hp接口测试工具.portalone;
using System.Net;
using System.IO;
//using System.Threading.AutoResetEvent;
//using System.Threading.ManualResetEvent_hevt_stop_event;
//using System.Threading.ManualResetEvent_hevt_socket_closed;


namespace hp接口测试工具
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            #region test
            //AuthenticateDownloadReq req = new AuthenticateDownloadReq();


            //HttpWebRequest request ;

            //request = (HttpWebRequest)HttpWebRequest.Create("接口地址");

            ////request = (HttpWebRequest)HttpWebRequest.Create("http://www.baidu.com");

            //request.Method = "POST";


            //HttpWebResponse response;

            //response = (HttpWebResponse)request.GetResponse();

            //Stream s = response.GetResponseStream();

            //StreamReader reader = new StreamReader(s, Encoding.UTF8);

            //string ss = reader.ReadToEnd();
            #endregion
            //构造soap请求信息
            StringBuilder soap = new StringBuilder();
            soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            soap.Append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
            soap.Append("<soap:Body>");
            soap.Append("<getWeather xmlns=\"http://WebXml.com.cn/\">");
            soap.Append("<theCityCode>2210</theCityCode>");
            soap.Append("<theUserID></theUserID>");
            soap.Append("</getWeather>");
            soap.Append("</soap:Body>");
            soap.Append("</soap:Envelope>");


            #region soap请求报文
            //soap.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            //soap.AppendLine("<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://地址\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">");
            //soap.AppendLine("<soapenv:Header/>");
            //soap.AppendLine("<soapenv:Body>");
            //soap.AppendLine("<ser:authenticateDownload soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
            //soap.AppendLine("<authenticateDownloadReq xsi:type=\"req:AuthenticateDownloadReq\" xmlns:req=\"http://地址\">");
            //soap.AppendLine("<accessChannelID xsi:type=\"xsd:string\">?</accessChannelID>");
            //soap.AppendLine("<countryCode xsi:type=\"xsd:string\">?</countryCode>");
            //soap.AppendLine("<deviceID xsi:type=\"xsd:string\">?</deviceID>");
            //soap.AppendLine("<language xsi:type=\"xsd:string\">?</language>");
            //soap.AppendLine("<security xsi:type=\"xsd:string\">?</security>");
            //soap.AppendLine("<timestamp xsi:type=\"xsd:string\">?</timestamp>");
            //soap.AppendLine("<userID xsi:type=\"xsd:string\">?</userID>");
            //soap.AppendLine("<cardID xsi:type=\"xsd:string\">?</cardID>");
            //soap.AppendLine("<chargingID xsi:type=\"xsd:string\">?</chargingID>");
            //soap.AppendLine("<contentCode xsi:type=\"xsd:string\">?</contentCode>");
            //soap.AppendLine("<extensionInfo xsi:type=\"ser:ArrayOf_tns2_NamedParameter\" soapenc:arrayType=\"bean:NamedParameter[]\" xmlns:bean=\"http://地址\"/>");
            //soap.AppendLine("<instance xsi:type=\"xsd:string\">?</instance>");
            //soap.AppendLine("<isTry xsi:type=\"xsd:string\">?</isTry>");
            //soap.AppendLine("<packageID xsi:type=\"xsd:string\">?</packageID>");
            //soap.AppendLine("<payType xsi:type=\"xsd:string\">?</payType>");
            //soap.AppendLine("<portalChannelID xsi:type=\"xsd:string\">?</portalChannelID>");
            //soap.AppendLine("<presentId xsi:type=\"xsd:string\">?</presentId>");
            //soap.AppendLine("<recommendID xsi:type=\"xsd:string\">?</recommendID>");
            //soap.AppendLine("<saleChannelId xsi:type=\"xsd:string\">?</saleChannelId>");
            //soap.AppendLine("<saleChannelURL xsi:type=\"xsd:string\">?</saleChannelURL>");
            //soap.AppendLine("<serviceID xsi:type=\"xsd:string\">?</serviceID>");
            //soap.AppendLine("<shortAddr xsi:type=\"xsd:string\">?</shortAddr>");
            //soap.AppendLine("<ua xsi:type=\"xsd:string\">?</ua>");
            //soap.AppendLine("<useType xsi:type=\"xsd:string\">?</useType>");
            //soap.AppendLine("</authenticateDownloadReq>");
            //soap.AppendLine("</ser:authenticateDownload>");
            //soap.AppendLine("</soapenv:Body>");
            //soap.AppendLine("</soapenv:Envelope>");
            #endregion

            //string soaprequest = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.storefront.portalone.huawei.com\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\"><soapenv:Header/><soapenv:Body><ser:authenticateDownload soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><authenticateDownloadReq xsi:type=\"req:AuthenticateDownloadReq\" xmlns:req=\"http://request.service.storefront.portalone.huawei.com\"><accessChannelID xsi:type=\"xsd:string\">?</accessChannelID><countryCode xsi:type=\"xsd:string\">?</countryCode><deviceID xsi:type=\"xsd:string\">?</deviceID><language xsi:type=\"xsd:string\">?</language><security xsi:type=\"xsd:string\">?</security><timestamp xsi:type=\"xsd:string\">?</timestamp><userID xsi:type=\"xsd:string\">?</userID><cardID xsi:type=\"xsd:string\">?</cardID><chargingID xsi:type=\"xsd:string\">?</chargingID><contentCode xsi:type=\"xsd:string\">?</contentCode><extensionInfo xsi:type=\"ser:ArrayOf_tns2_NamedParameter\" soapenc:arrayType=\"bean:NamedParameter[]\" xmlns:bean=\"http://bean.service.storefront.portalone.huawei.com\"/><instance xsi:type=\"xsd:string\">?</instance><isTry xsi:type=\"xsd:string\">?</isTry><packageID xsi:type=\"xsd:string\">?</packageID><payType xsi:type=\"xsd:string\">?</payType><portalChannelID xsi:type=\"xsd:string\">?</portalChannelID><presentId xsi:type=\"xsd:string\">?</presentId><recommendID xsi:type=\"xsd:string\">?</recommendID><saleChannelId xsi:type=\"xsd:string\">?</saleChannelId><saleChannelURL xsi:type=\"xsd:string\">?</saleChannelURL><serviceID xsi:type=\"xsd:string\">?</serviceID><shortAddr xsi:type=\"xsd:string\">?</shortAddr><ua xsi:type=\"xsd:string\">?</ua><useType xsi:type=\"xsd:string\">?</useType></authenticateDownloadReq></ser:authenticateDownload></soapenv:Body></soapenv:Envelope>";

            string url = "http://www.webxml.com.cn/WebServices/WeatherWS.asmx";

            string result = GetSOAPReSource(url, soap.ToString());
         
          
        }

        public static string GetSOAPReSource(string url, string datastr)
        {

            //发起请求
            Uri uri = new Uri(url);
            WebRequest webRequest = WebRequest.Create(uri);
            webRequest.ContentType = "text/xml; charset=utf-8";
            webRequest.Method = "POST";
            using (Stream requestStream = webRequest.GetRequestStream())
            {
                byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString());
                requestStream.Write(paramBytes, 0, paramBytes.Length);
            }

            //响应
            WebResponse webResponse = webRequest.GetResponse();
            using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
            {
                string result = "";
                return result = myStreamReader.ReadToEnd();
            }
        }    
    }
}

原创粉丝点击