http://www.dotblogs.com.tw/antony77/archive/2010/04/05/14417.aspx
使用 HttpWebRequest 來作
http://www.coolsun.idv.tw/modules/xhnewbb/viewtopic.php?topic_id=1273
http://coding.anyun.tw/2012/02/22/using-httpwebrequest-post-data/
http://www.dotblogs.com.tw/joysdw12/archive/2012/12/04/85380.aspx
//取得當前網址及路徑
string urlPath = "";
string[] segments = Request.Url.Segments;
for (int i = 0; i <= segments.Length - 2; i++)
{
urlPath = urlPath + segments[i];
}
string bankUrl = string.Format("http://{0}:{1}{2}RedirectHandler.ashx", Request.Url.Host, Request.Url.Port, urlPath);
String parm = "BillerSeqNo=2014052361106509&Currency=NTD&Amount=1235500";
HttpTool http = new HttpTool();
NameValueCollection ackColl = http.SendRequest(bankUrl, parm);
if (ackColl == null)
{
string alertScript = "處理失敗,請重新操作!<br />(ACK Null)";
return;
}
string parm1 = ackColl["BillerSeqNo"];
string parm2 = ackColl["Currency"];
HttpTool.cs
using System;
using System.Web;
using System.Text;
using System.Net;
using System.IO;
using System.Collections.Specialized;
public class HttpTool
{
public NameValueCollection SendRequest(string iTargetUrl, string iParam)
{
//此處的編碼要注意,是否會錯編 "&" 及 "=" 符號
byte[] postData = Encoding.UTF8.GetBytes(iParam);
HttpWebRequest request = HttpWebRequest.Create(iTargetUrl) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 10000;
request.ContentLength = postData.Length;
request.AllowAutoRedirect = false; // 禁止重新導向網頁
// 寫入 Post Body Message 資料流
using (Stream st = request.GetRequestStream())
{
st.Write(postData, 0, postData.Length);
}
NameValueCollection qsColl = null;
// 取得回應資料
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode == HttpStatusCode.Found) // 判斷是否為 302
{
// 取得返回的Header中的Location參數,Location含有重新導向的網址資訊
Uri redirectUrl = new Uri(response.Headers["Location"]);
// 回傳全部參數的集合
// Parse the query string variables into a NameValueCollection.
qsColl = HttpUtility.ParseQueryString(redirectUrl.Query, Encoding.UTF8);
}
}
return qsColl;
}
}
RedirectHandler.ashx
using System;
using System.Web;
using System.Collections.Specialized;
public class RedirectHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string url = @"http://127.0.0.1/AppPayCB/PaymentResult.aspx";
NameValueCollection pColl = context.Request.Params;
string all = string.Format(@"{0}?BillerSeqNo={1}&Currency={2}", url, pColl["BillerSeqNo"], pColl["Currency"]);
context.Response.Redirect(all);
}
public bool IsReusable
{
get
{
return false;
}
}
}