Request请求https webservice

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml;

namespace ConsoleApp9
{
    class Program
    {
     
        static void Main(string[] args)
        {

            string fileName = @"D:\Project\需求\标牌\D2102270011.xml";

            /*
             
             <CertificateData sTestGuid="1e6c79e0-b70f-44a6-8922-03bcd1ff9a20" dtTest="2022/05/19" bLRib="FALSE" bRebendTest="TRUE" bBendTest="FALSE" dElongationAGT="8.7" dElongationAS="" dRatio="1.17" dTensileStrength="655" dYieldStrength="560" dMassPerMeter="0" dArea="" lTestLineNumber="1" dCarbocEquiv="0.3340" dChemCompSn="0.0010" dChemCompAl="0.0020" dChemCompB="0.0010" dChemCompN="0.0038" dChemCompNb="0.0020" dChemCompV="0.0030" dChemCompCu="0.0140" dChemCompNi="0.0020" dChemCompMo="0.0010" dChemCompCr="0.3550" dChemCompS="0.0260" dChemCompP="0.0220" dChemCompMn="0.4820" dChemCompSi="0.2670" dChemCompC="0.1810" sSteelProcess="BOF / Concast / Hot Rolling" lLength="3500" dDiam="0" lFinishing="D2102270011" lFormat="D2102270011" lStandard="D2102270011" sProductDescription="" sDigitalRecordGUID="069033514789415CA19942102C052639" sCastNumber="D2102270011" sCertificateNumber="" lRollingMill="D2102270011" />
  
             */
            string xmlContent = File.ReadAllText(fileName);


            var methodName = "ProcessRequest";

            string url = "https://api.cares.cloud/SoapServices/ProcessRequestService.asmx?WSDL";
             
            var resquest = new Request();
            var response = resquest.GetUrlResponse(url);

            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            XmlDocument doc1 = new XmlDocument();
            doc1.LoadXml(sr.ReadToEnd());
            sr.Close();
            response.Close();

            var XmlNs = doc1.SelectSingleNode("//@targetNamespace").Value;
            //Console.WriteLine(XmlNs);

            XmlDocument doc = new XmlDocument();
            String s1 = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope\""+
                  " xmlns:crs=\""+XmlNs+"\" >" +

                "</soapenv:Envelope>";

            //Console.WriteLine(s1);
            doc.LoadXml(
              s1
              );
            XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.InsertBefore(decl, doc.DocumentElement);
             
            XmlNode soapHeader = doc.CreateElement("soapenv", "Header", doc.DocumentElement.NamespaceURI);

            XmlNode soapHeader_1 = doc.CreateElement("crs", "TokenHeader", doc.DocumentElement.NamespaceURI);

        
            XmlNode soapHeader_2 = doc.CreateElement("crs", "Token", doc.DocumentElement.NamespaceURI);

            soapHeader_2.InnerText = "6944AA00937523D9FA2911CAFE8F401916C17FA849BDC1702C5EBF1DA637178BF446DAD7F5582D12ABE4B1484F81F11E745C3C55342EC408488282A80E76A8EA";

            soapHeader_1.AppendChild(soapHeader_2);
            soapHeader.AppendChild(soapHeader_1);

            doc.DocumentElement.AppendChild(soapHeader); 

            XmlElement soapBody = doc.CreateElement("soapenv",
               "Body", doc.DocumentElement.NamespaceURI);

            XmlElement soapMethod = doc.CreateElement("crs", methodName, doc.DocumentElement.NamespaceURI); 


            XmlElement certificatesData = doc.CreateElement("crs", "CertificatesData", doc.DocumentElement.NamespaceURI);

            certificatesData.InnerXml = xmlContent;

            soapMethod.AppendChild(certificatesData);
           soapBody.AppendChild(soapMethod);
            doc.DocumentElement.AppendChild(soapBody);

            Console.WriteLine(doc.OuterXml);

            File.WriteAllText("request.xml", doc.OuterXml);

            byte[] data = Encoding.UTF8.GetBytes(doc.OuterXml);


            var reqProcess = new Request().Process(url, data);


            Console.WriteLine(reqProcess);
          

        }
    }


    public class Request
    {
        private bool CheckValidationResult(object sender,
        X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;// Always accept
        }



        public string Process(string url,byte[] data)
        {

            HttpWebResponse resp = null;
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
            var request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "text/xml; charset=utf-8";
            request.ContentLength = data.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(data, 0, data.Length);
            writer.Close();

            using (resp = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader sr_b = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8))
                {
                    return sr_b.ReadToEnd();
                }
                   
            }




        }

        public HttpWebResponse GetUrlResponse(string url)
        {

            
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
            var  request = WebRequest.Create(url) as HttpWebRequest;
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

                return resp;


           
           
        }
    }
}

  

posted on 2022-08-05 10:28  陈-chen  阅读(67)  评论(0编辑  收藏  举报