string cnpUrl = "http://127.0.0.1:9000/cnp/purchase";//提交到proxy
string cnpReqXml ="";//发送的数据字符串
HttpWebRequest cnpReq = (HttpWebRequest)WebRequest.Create(cnpUrl);
byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(cnpReqXml);//转换xml内的数据为byte的数组
cnpReq.Method = "POST";
cnpReq.ContentType = "application/x-www-form-urlencoded";
cnpReq.ContentLength = requestBytes.Length;
using (Stream requestStream = cnpReq.GetRequestStream())
{
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
}
//接收TR2返回XML数据
HttpWebResponse cnpRes;
try
{
cnpRes = (HttpWebResponse)cnpReq.GetResponse();
}
catch (WebException ex)
{
cnpRes = (HttpWebResponse)ex.Response;
}
if (cnpRes.StatusCode != HttpStatusCode.OK)
{
string message = String.Format("POST failed. Received HTTP {0}",
cnpRes.StatusCode);
throw new ApplicationException(message);
}
StreamReader cnpStr = new StreamReader(cnpRes.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
string backstr = cnpStr.ReadToEnd();
StreamWriter cnpStw = File.CreateText("生成的文件名");
cnpStw.WriteLine(backstr);//生成XML文件
cnpStw.Flush();
cnpStw.Close();
cnpRes.Close();
//读取XML文档的节点
try
{
XmlDocument cnpXmlDoc = new XmlDocument();
cnpXmlDoc.Load(Generation);
XmlNamespaceManager manager = new XmlNamespaceManager(cnpXmlDoc.NameTable);
manager.AddNamespace("MyAPI", "http://www.99bill.com/mas_cnp_merchant_interface");
XmlNode cnpNode = cnpXmlDoc.SelectSingleNode("//MyAPI:responseCode", manager);
XmlNode cnpNodeOrderId = cnpXmlDoc.SelectSingleNode("//MyAPI:externalRefNumber", manager);
XmlNode cnpRefNumber = cnpXmlDoc.SelectSingleNode("//MyAPI:refNumber", manager);
string refNumber = cnpRefNumber.InnerText;
string myXml = cnpNode.InnerText;
}
......