.net http post xml 例子

 

http+post+xml

html:

  
名称:


密匙:


时间:" />

code:

protected void Page_Load(object sender, EventArgs e)
{
 if (HttpContext.Current.Request.HttpMethod.Equals("POST"))
        {
 IDictionary parameters = PrepareParameters();
 XmlDocument document = GetXmlDocument(parameters);
 string url = GetApplicationUrl() + "/userinfo.ashx";
 WebRequest request = WebRequest.Create(url);
        request.Method = "POST";

          using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(document.InnerXml);
            }

            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    Response.Write(reader.ReadToEnd());
                    Response.End();
                }
            }
 }
}

 private IDictionary Parameters()
    {
        Dictionary parameters = new Dictionary();
        parameters["username"] = Request.Form["username"];       
        parameters["key"] = Request.Form["key"];       
        parameters["date"] = Request.Form["date"];
        return parameters;
    }

 private XmlDocument GetXmlDocument(IDictionary parameters)
    {
        XmlDocument document = new XmlDocument();
        document.LoadXml("");

        foreach (KeyValuePair param in parameters)
        {
            XmlNode node = document.CreateElement(param.Key);
            node.InnerText = param.Value;

            document.DocumentElement.AppendChild(node);
        }

        return document;
    }


web.config

   path="userinfo.ashx"
 type="MyHandler.userinfoHandler, MyHandler"/>

namespace MyHandler {
  public class MyHandler  : IHttpHandler
     {
 
 ResourceManager resource;
 

#region IHttpHandler 成员

        public bool IsReusable
        {
            get { return false; }
        }

 public void ProcessRequest(HttpContext context)
        {
  context.Response.ContentType = "text/xml";
  string xmlString = "";
  
  //xml xsd格式验证
  XmlDocument document = new XmlDocument();
  
  //获取bll文件夹Resources的userinfoScheme.xsd 的内容,见下文
  ResourceManager resource = new ResourceManager("BLL.Resources", typeof(Global).Assembly);
  string schemeText = resource.GetObject("userinfoScheme") as string;

  using (StreamReader requestReader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding)) {
  using (StringReader schemeReader = new StringReader(schemeText)) {
     XmlReader scheme = XmlReader.Create(schemeReader, new XmlReaderSettings() {
      IgnoreWhitespace = true,
      ValidationType = ValidationType.Schema
     });

     document.Load(requestReader);
     document.Schemas.Add("http://tempuri.org/userinfo.xsd", scheme);
     document.Validate((sender, e) => {
      throw new Exception(e.Message, e.Exception);
     });
    }
   }

   StringBuilder builder = new StringBuilder();
                builder.Append("");
                builder.Append("");
                builder.Append("xml demo");
                builder.Append("
");
                xmlString = builder.ToString();
  
  context.Response.Write(xmlString);
         context.Response.End();
 }
 #endregion

 }
}


xsd 的内容


     targetNamespace="http://tempuri.org/userinfo.xsd"
     elementFormDefault="qualified"
     xmlns="http://tempuri.org/userinfo.xsd"
     xmlns:mstns="http://tempuri.org/userinfo.xsd"
     xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
  
   
            type="xs:string"
       minOccurs="1"
       maxOccurs="1"/>
            type="xs:string"
       minOccurs="1"
       maxOccurs="1"/>
            type="xs:string"
       minOccurs="1"
       maxOccurs="1"/>
    
            type="xs:string"
       minOccurs="1"
       maxOccurs="1"/>
   

  

 


你可能感兴趣的:(Asp.Net)