VPOS MAS-CNP接口——建立https连接发送请求并接收返回的XML数据流

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;

             }

            ......
                               

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值