C#_xml文件请求及接收数据

  • Post author:
  • Post category:其他



String xml = “<data>中文</data>”;

String postData = “data=” + Server.UrlEncode(xml); ;

string strUrl = ”

http://www.etu6.com/api/lines.xml

“;

// 准备请求…

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);

myRequest.Method = “POST”;

myRequest.ContentType = “application/x-www-form-urlencoded”;

myRequest.ContentLength = postData.Length;

Stream newStream = myRequest.GetRequestStream();

byte[] data = Encoding.GetEncoding(“GB2312”).GetBytes(postData);

// 发送数据

newStream.Write(data, 0, data.Length);

HttpWebResponse res = myRequest.GetResponse() as HttpWebResponse;

StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding(“GB2312”));

String ret = sr.ReadToEnd();

newStream.Close();

Response.Write(“返回的数据是:” + Server.HtmlEncode(ret));

//Response.ClearContent();

//Response.Write(Request.Form[“data”]);

Response.End();