.net http post xml 例子

 

http+post+xml

html:

<div>  
名称:<input type="text" name="username" value="13606136600" />
<br>
密匙:<input type="text" name="key" value="zzzzz" />
<br>
时间:<input type="text" name="date" value="<%= DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") %>" />

</div>

code:

protected void Page_Load(object sender, EventArgs e)
{
 if (HttpContext.Current.Request.HttpMethod.Equals("POST"))
        {
 IDictionary<string, string> 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<string, string> Parameters()
    {
        Dictionary<string, string> parameters = new Dictionary<string, string>();
        parameters["username"] = Request.Form["username"];       
        parameters["key"] = Request.Form["key"];       
        parameters["date"] = Request.Form["date"];
        return parameters;
    }

 private XmlDocument GetXmlDocument(IDictionary<string, string> parameters)
    {
        XmlDocument document = new XmlDocument();
        document.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");

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

            document.DocumentElement.AppendChild(node);
        }

        return document;
    }


web.config
<httpHandlers>
 <add verb="*"
 path="userinfo.ashx"
 type="MyHandler.userinfoHandler, MyHandler"/>
</httpHandlers>

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("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                builder.Append("<root>");
                builder.Append("<content>xml demo</content>");
                builder.Append("</root>");
                xmlString = builder.ToString();
  
  context.Response.Write(xmlString);
         context.Response.End();
 }
 #endregion

 }
}


xsd 的内容

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="inviteScoreScheme"
     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">
 <xs:element name="root">
  <xs:complexType>
   <xs:all>
    <xs:element name="username"
       type="xs:string"
       minOccurs="1"
       maxOccurs="1"/>
    <xs:element name="key"
       type="xs:string"
       minOccurs="1"
       maxOccurs="1"/>
    <xs:element name="date"
       type="xs:string"
       minOccurs="1"
       maxOccurs="1"/>
    
    <xs:element name="skey"
       type="xs:string"
       minOccurs="1"
       maxOccurs="1"/>
   </xs:all>
  </xs:complexType>
 </xs:element>
</xs:schema>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值