http+post+xml
html:
code:
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Request.HttpMethod.Equals("POST"))
{
IDictionary
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
{
Dictionary
parameters["username"] = Request.Form["username"];
parameters["key"] = Request.Form["key"];
parameters["date"] = Request.Form["date"];
return parameters;
}
private XmlDocument GetXmlDocument(IDictionary
{
XmlDocument document = new XmlDocument();
document.LoadXml("
foreach (KeyValuePair
{
XmlNode node = document.CreateElement(param.Key);
node.InnerText = param.Value;
document.DocumentElement.AppendChild(node);
}
return document;
}
web.config
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("
builder.Append("
xmlString = builder.ToString();
context.Response.Write(xmlString);
context.Response.End();
}
#endregion
}
}
xsd 的内容
elementFormDefault="qualified"
xmlns="http://tempuri.org/userinfo.xsd"
xmlns:mstns="http://tempuri.org/userinfo.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
minOccurs="1"
maxOccurs="1"/>
minOccurs="1"
maxOccurs="1"/>
minOccurs="1"
maxOccurs="1"/>
minOccurs="1"
maxOccurs="1"/>