2017年2月28日 星期二

How to programmatically POST data to an aspx page

參考引用 : How to programmatically POST data to an aspx page
---
string url = "https://www.paygate.co.za/PayWebv2/process.trans";


         

            StringBuilder postData = new StringBuilder();


            postData.Append("PAYGATE_ID=" + HttpUtility.UrlEncode(PAYGATE_ID.Text) + "&");
            postData.Append("REFERENCE=" + HttpUtility.UrlEncode(REFERENCE.Text) + "&");
            postData.Append("AMOUNT=" + HttpUtility.UrlEncode(AMOUNT.Text) + "&");
            postData.Append("CURRENCY=" + HttpUtility.UrlEncode(CURRENCY.Text) + "&");
            postData.Append("RETURN_URL=" + HttpUtility.UrlEncode(RETURN_URL.Text) + "&");
            postData.Append("TRANSACTION_DATE=" + HttpUtility.UrlEncode(TRANSACTION_DATE.Text) + "&");
            postData.Append("CHECKSUM =" + HttpUtility.UrlEncode(CHECKSUM.Text) + "&");
            postData.Append("EMAIL =" + HttpUtility.UrlEncode(CHECKSUM.Text) + "&");
            postData.Append("encryption_key=" + HttpUtility.UrlEncode(encryption_key.Text));
            //ETC for all Form Elements


            // Now to Send Data.
            StreamWriter writer = null;


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postData.ToString().Length;
            try
            {
                writer = new StreamWriter(request.GetRequestStream());
                writer.Write(postData.ToString());


            HttpWebResponse WebResp = (HttpWebResponse)request.GetResponse();
                   

            //Now, we read the response (the string), and output it.
            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            Console.WriteLine(_Answer.ReadToEnd());


            }
            finally
            {
                if (writer != null)
                    writer.Close();
            }

沒有留言:

張貼留言