string postData = string.Format("ActionCode=Sms01&UserName=mine&Pwd=123&Mobiles={0}&Content={1}", strMobiles, strMessage+ "【客服中心】");
并把请求转换成字节格式
byte[] data = Encoding.Default.GetBytes(postData);
2、 用HttpWebRequest来发送建立请求
HttpWebRequest myrequest = (HttpWebRequest)WebRequest.Create(SERVICE_URL);
3、向请求中写入请求的相关属性,也就是请求的头部格式(请求方法,版本类型,请求的字符编码,响应时间等)
myrequest.Method = "POST";
myrequest.ContentType = "application/x-www-form-urlencoded";
myrequest.ContentLength = data.Length;
4、 向请求中写入请求信息
using (Stream datasteam = myrequest.GetRequestStream())
{
datasteam.Write(data, 0, data.Length);
datasteam.Close();
}
5、得到响应请求
HttpWebResponse myResponse = myrequest.GetResponse() as HttpWebResponse;
6、读取响应中的数据
HttpWebResponse myResponse = myrequest.GetResponse() as HttpWebResponse;
using (StreamReader reader = new StreamReader(
myResponse.GetResponseStream()
, System.Text.Encoding.GetEncoding("utf-8")))
{
response = reader.ReadToEnd();
xmlDoc.LoadXml(response);
reader.Close();
}
一般从网站返回的是一段xml格式的字符串
7、把该字符串转换成xml格式,并取出当中的相关数据
XmlDocument xmlDoc = new System.Xml.XmlDocument();
string result = xmlDoc.SelectSingleNode("ReturnInfo").InnerText;