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>