用post方法完成网站之间xml的传递

达到的效果是:在提交页面上将一个xml传递到另外一个网站的某个接口上,从接口上返回一个接收到的标志

本网站提交页面
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            XmlDocument Dom = BuildXml();
            string url = "http://192.168.3.110:8022/InterFace.aspx";
            Dom = XmlTransfer(url, Dom);
            Response.Write(Dom.SelectSingleNode("/Response/Result").InnerText);

        }


        /// <summary>
        /// 动态建立一个xml文档
        /// </summary>
        /// <returns></returns>
        protected XmlDocument BuildXml()
        {
            XmlDocument Dom = new XmlDocument();
            XmlNode root = Dom.CreateElement("Request");//建立根节点
            XmlNode node = Dom.CreateElement("Products");//建立子节点
            XmlAttribute att = Dom.CreateAttribute("ID");
            att.InnerText = "01";
            node.Attributes.Append(att);
            node.InnerText = "31";
            root.AppendChild(node);

            node = Dom.CreateElement("UserLogin");//建立子节点
            node.InnerText = "测试";
            root.AppendChild(node);

            node = Dom.CreateElement("UserType");//建立子节点
            node.InnerText = "01";
            root.AppendChild(node);
            Dom.AppendChild(root);

            return Dom;
 
        }

        /// <summary>
        /// 向该地址传递xml,并取得该地址返回回来的xml
        /// </summary>
        /// <param name="url"></param>
        /// <param name="dom"></param>
        /// <returns></returns>
        protected XmlDocument XmlTransfer(string url, XmlDocument dom)
        {
            System.Net.WebClient xmlHttp = new System.Net.WebClient();

            string str = (System.Text.Encoding.UTF8.GetString(xmlHttp.UploadData(url, "post", System.Text.Encoding.UTF8.GetBytes(dom.InnerXml))));

            XmlDocument domReturn = new XmlDocument();
            dom.LoadXml(str);

            return dom;
        }

 

    }
}




另一个网站的接口页(http://192.168.3.110:8022/InterFace.aspx

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace WebApplication1
{
    public partial class InterFace : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.IO.Stream st = Request.InputStream;
            System.Xml.XmlDocument Dom = new XmlDocument();
            Dom.Load(st);
            string aa = Dom.SelectSingleNode("/Request/UserLogin").InnerText;
            if (aa.Length > 0)
            {
                RequestSuc();
            }

        }

        public static void RequestSuc()
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "text/xml";
            System.Web.HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.UTF8;
            XmlDocument Dom = new XmlDocument();
            XmlNode node, tmpNode;
            node = Dom.CreateElement("Response");
            tmpNode = Dom.CreateElement("Result");
            tmpNode.InnerText = "true";
            node.AppendChild(tmpNode);
            tmpNode = Dom.CreateElement("Message");
            tmpNode.InnerText = "";
            node.AppendChild(tmpNode);
            Dom.AppendChild(node);
            System.Web.HttpContext.Current.Response.Write(Dom.InnerXml);
            System.Web.HttpContext.Current.Response.End();
        }
    }
}


你可能感兴趣的:(post)