日期:2014-05-18 浏览次数:20430 次
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(); } }
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 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>