如何用ASP.net將參數進行Post提交

來源:互聯網
上載者:User

 

有很多頁面是不能夠直接存取的,往往是由於session或者cookie控制了,需要使用者名稱和密碼登入後才可以看到,怎麼用Asp.net程式直接擷取需要驗證的頁面?下面這個函數可以實現:

protected static string cookieheader;
        public string Login(String url, String paramList) 
        {
            HttpWebResponse res = null;
            string strResult="";

            try 
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                req.AllowAutoRedirect = false;
                CookieContainer cookieCon = new CookieContainer();
                req.CookieContainer = cookieCon;

                StringBuilder UrlEncoded = new StringBuilder();
                Char[] reserved = {'?', '=', '&'};
                byte[] SomeBytes = null;

                if (paramList != null) 
                {
                    int i=0, j;
                    while(i<paramList.Length)
                    {
                        j=paramList.IndexOfAny(reserved, i);
                        if (j==-1)
                        {
                            UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length-i)));
                            break;
                        }
                        UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j-i)));
                        UrlEncoded.Append(paramList.Substring(j,1));
                        i = j+1;
                    }
                    SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
                    req.ContentLength = SomeBytes.Length;
                    Stream newStream = req.GetRequestStream();
                    newStream.Write(SomeBytes, 0, SomeBytes.Length);
                    newStream.Close();
                } 
                else 
                {
                    req.ContentLength = 0;
                }

                res = (HttpWebResponse)req.GetResponse();
                cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url));
                HttpContext.Current.Application.Lock();
                HttpContext.Current.Application["cookieheader"] = cookieheader;
                HttpContext.Current.Application.UnLock();

                Stream ReceiveStream = res.GetResponseStream();
                Encoding encode = System.Text.Encoding.UTF8;
                StreamReader sr = new StreamReader( ReceiveStream, encode );
                Char[] read = new Char[256];
                int count = sr.Read( read, 0, 256 );
                while (count > 0) 
                {
                    String str = new String(read, 0, count);
                    strResult += str;
                    count = sr.Read(read, 0, 256);
                }
            } 
            catch(Exception e) 
            {
                strResult = e.ToString();
            } 
            finally 
            {
                if ( res != null ) 
                {
                    res.Close();
                }
            }
            strResult = strResult.Replace("\r\n","");
            return strResult;
        }

參數說明:url表示提交的目標頁面。
                paramList表示參數列表(格式如:param1=123&parma2=456)
               
使用例子:
 void LoginSample()
  {
   if (HttpContext.Current.Application["cookieheader"] != null)
   {
    cookieheader = (string)HttpContext.Current.Application["cowokieheader"];
   }
   else
   {
    Login("http://www.myule.com/login.asp", "user=likef&password=likef&Next= 登 錄 ");
   }

運行上面函數就會產生一個cookie,然後就可以輕而易舉得後需要驗證的頁面內容了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.