zoukankan      html  css  js  c++  java
  • 【转】根据URL来读取网页输出的数据

    读取网页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();
            }
  • 相关阅读:
    HTML DOM 06 节点关系
    HTML DOM 05 事件(三)
    HTML DOM 05 事件(二)
    HTML DOM 05 事件(一)
    html DOM 04 样式
    html DOM 03 节点的属性
    html DOM 02 获取节点
    html DOM 01 节点概念
    JavaScript 29 计时器
    JavaScript 28 弹出框
  • 原文地址:https://www.cnblogs.com/gzh4455/p/2582420.html
Copyright © 2011-2022 走看看