欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Netcore webservice

程序员文章站 2023-12-28 13:41:58
...
using System;
using System.Text;
using System.Net;
using System.Net.Http;
using System.IO;

namespace WebSeviceTest
{
    class Program
    {
        private static string head = "<?xml version =\"1.0\" encoding=\"utf-8\"?>";
        private static string P1 = "<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:Body><GetProductionData xmlns=\"http://tempuri.org/\">";


        private static string end = "</GetProductionData></soap:Body></soap:Envelope>";
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            GetData("74100001", "2019-03-01", "2019-04-01");
        }

        private static string GetData(string weeks,string pstart,string pend)
        {
            string rsp = "";
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(head);
                sb.Append(P1);
                sb.AppendFormat("<AA>{0}</AA>", weeks);
                sb.AppendFormat("<BB>{0}</BB>", pstart);
                sb.AppendFormat("<CC>{0}</CC>", pend);
                sb.Append(end);

                Uri uri = new Uri("http://127.0.0.1/xxx.asmx");
                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(sb.ToString());
                    requestStream.Write(paramBytes, 0, paramBytes.Length);
                }

                //响应
                WebResponse webResponse = webRequest.GetResponse();
                using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                {
                    Console.WriteLine(myStreamReader.ReadToEnd());
                    Console.WriteLine("Success...");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            

            return rsp;

        }
    }
}

 

上一篇:

下一篇: