using System;
using System.Text;
using System.Net;
using System.Net.Http;
using System.IO;
using System.Xml;
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><GetCCC xmlns=\"http://tempuri.org/\">";
private static string end = "</GetCCC></soap:Body></soap:Envelope>";
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
// 2018 / 6 / 5 11:07:36 GetData("CXXX", "2019-03-01 08:00:00", "2019-03-1 24:00:00");
GetData("XXX2", "2019/03/11 00:00:00", "2019/03/11 23:59:59");
}
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("<AAA>{0}</AAA>", weeks);
sb.AppendFormat("<BBB>{0}</BBB>", pstart);
sb.AppendFormat("<CCC>{0}</CCC>", pend);
sb.Append(end);
Uri uri = new Uri("http://127.0.0.1/TEST.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))
{
string res = myStreamReader.ReadToEnd();
Console.WriteLine(res);
XmlDocument doc = new XmlDocument( );
doc.LoadXml(res);
XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
XmlNodeList list = doc.SelectNodes("//Table");
Console.WriteLine("Count "+list.Count);
foreach (XmlNode item in list)
{
Console.WriteLine(item.SelectSingleNode("QUANTITY").InnerXml);
// Console.WriteLine(item.Name + " "+item.InnerText);
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
return rsp;
}
}
}