读取网页Reponse数据,下面是一个浪驰短信的实例

 public static string HttpSMSPost(HttpWebRequest hr, string url, string parameters)
         {
            string strRet = null;
            ASCIIEncoding encoding = new ASCIIEncoding(); 
            byte[] data = encoding.GetBytes(parameters); 

            try
            {
                hr.KeepAlive = false;
                hr.Method = "POST";
                hr.ContentType = "application/x-www-form-urlencoded";
                hr.ContentLength = data.Length; 
                Stream newStream = hr.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                HttpWebResponse myResponse = (HttpWebResponse)hr.GetResponse();    
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
                strRet = reader.ReadToEnd();      
                reader.Close();
                myResponse.Close();

                return strRet;
        
            }
            catch (Exception ex)
            {
                strRet = "-200";
            }
            return strRet;
        }


使用:

        string url = http://test.com/Login.asp;
        string SMSparameters = "UserID=" + Userid + "&Account=" + Account + "&Password=" + PassWord;

        string targeturl = url.Trim().ToString();
        HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);
        hr.CookieContainer = cookieContainerSMS;
        string res = httpPost.HttpSMSPost(hr, url, SMSparameters);

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(res);
        string errorNum = xml.SelectSingleNode("/LANZ_ROOT/ErrorNum").InnerText;  //获取是否登陆成功

        ActiveID = xml.SelectSingleNode("/LANZ_ROOT/ActiveID").InnerText;  //获取成功后的ActiveID 
        if (errorNum == "0")
        {
            //this.Close();
            Response.Write("登陆成功");
        }
        else
        {
            Response.Write("<script>alert('登陆失败')</script>");
            //this.Close();
        }

 

posted on 2012-07-09 11:23  gzh4455  阅读(210)  评论(0编辑  收藏  举报