为做数据接口,发送XML请求至一个处理页面 ReOnly.aspx
ReOnly.aspx 前台页面为默认创建的内容
cs代码如下:
- C# code
public partial class ReOnly : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //设置 HTTP 头 //Response.ContentType = "text/xml"; //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.Clear(); XmlTextWriter writer = null; writer = new XmlTextWriter(Response.Output); writer.Formatting = Formatting.Indented; writer.WriteStartDocument(); writer.WriteStartElement("root"); writer.WriteStartElement("appid"); writer.WriteString("aaa"); writer.WriteEndElement(); writer.WriteStartElement("status"); writer.WriteString("0"); writer.WriteEndElement(); writer.WriteStartElement("needcookie"); writer.WriteString("2"); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); } }
请求的主要内容如下:
- C# code
WebRequest request = WebRequest.Create(url); request.Method = "POST"; string sendData = xmlDoc.InnerXml; byte[] byteArray = Encoding.Default.GetBytes(sendData); Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream, System.Text.Encoding.GetEncoding("GB2312")); string responseXml = reader.ReadToEnd(); //try //{ xml.LoadXml(responseXml);//这里提示了错误,说DTD内容应该在写根节点前,原因主要是返回了多余的html代码 string status = xml.DocumentElement.SelectSingleNode("status").InnerText; if (status == "0") { Message = "返回成功!"; } else { Message = "请求数据错误!"; }
xml.LoadXml 得到的代码
- XML code
<?xml version="1.0" encoding="utf-8"?><root> <appid>aaa</appid> <status>0</status> <needcookie>2</needcookie></root><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><body> <form name="form1" method="post" action="ReOnly.aspx" id="form1"><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZLqex1hYqVDdvl7couz8D8g1tCTz" /> <div> </div> </form></body></html>
我看把多余的 ReOnly.aspx 的 html 代码也给返回回来了,请问在返回XML 的时候要如何避免返回HTML 代码的内容呢?
谢谢!
------解决方案--------------------------------------------------------