2009-11-13 22 views
4

我試圖將Moneris Hosted Pay Page和我的.net 1.1應用程序與iFrame集成。我以前做過這麼多次,但沒有使用.net 1.1。我似乎無法找到用於編寫1.1版程序化HTML Post的良好資源。有什麼建議麼?使用C#.NET 1.1編程HTML POST

謝謝!

編輯:在給出的建議中發現,我意識到HttpWebRequest解決方案將無法工作,因爲您無法與POST一起執行重定向。爲了與Moneris正確集成,您必須發佈金額值,然後重定向到您已發佈的網址。對困惑感到抱歉。

+0

正如在一次發佈的迴應你回來是一個HTTP 301? – Kev 2009-11-13 19:42:54

回答

0

我要在這裏回答我的問題,以防萬一這得到由別人提及。基本上,我創建了一個自定義HTTP處理程序(.ashx),它生成一個獨立於.NET的HTML頁面,並帶有一個表單,用於POST所需的URL。基本上,一個動態構建的HTML頁面。這樣我可以動態地傳遞我的金額值,然後做一個正常的HTML POST。

+2

這是使用.NET完成HTTP Post的正確方法。如果您使用HttpWebRequest,服務器將收到響應並將其寫入客戶端。然後,第三方的Cross Site Scripting可以識別與結果表單的任何交互。通過在客戶端使用標準的HTTP表單發佈,用戶的瀏覽器被重定向到第三方站點。 – mwalsher 2009-11-13 20:31:31

1

HttpWebRequest類。這將允許您從頭開始構建HTTP請求,這將允許您將數據包含到您指定的URL中,並獲得響應。

2

您可以使用HttpWebRequestHttpWebResponse類來實現此目的。

例如,張貼到有兩個字段,username HTML表單和password你會做這樣的事情:

NameValueCollection nv = new NameValueCollection(); 
nv.Add("username", "bob"); 
nv.Add("password", "password"); 

string method = "POST"; // or GET 
string url = "http://www.somesite.com/form.html"; 

HttpStatusCode httpStatusCode; 
string response = SendHTTPRequest(nv, method, url, out httpStatusCode); 


public static string SendHTTPRequest(NameValueCollection data, 
     string method, 
     string url, 
     out HttpStatusCode httpStatusCode) 
{ 
    StringBuilder postData = new StringBuilder(); 
    foreach(string key in data) 
    { 
    postData.Append(key + "=" + data[key] + "&"); 
    } 

    if(method == "GET" && data.Count > 0) 
    { 
    url += "?" + postData.ToString(); 
    } 

    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
    httpWebRequest.Method = method; 
    httpWebRequest.Accept = "*/*"; 
    httpWebRequest.ContentType = "application/x-www-form-urlencoded"; 

    if(method == "POST") 
    { 
    using(Stream requestStream = httpWebRequest.GetRequestStream()) 
    { 
     using(MemoryStream ms = new MemoryStream()) 
     { 
     using(BinaryWriter bw = new BinaryWriter(ms)) 
     { 
      bw.Write(Encoding.GetEncoding(1252).GetBytes(postData.ToString())); 
      ms.WriteTo(requestStream); 
     } 
     } 
    } 
    } 

    return GetWebResponse(httpWebRequest, out HttpStatusCode httpStatusCode); 
} 

private static string GetWebResponse(HttpWebRequest httpWebRequest, 
      out HttpStatusCode httpStatusCode) 
{ 
    using(HttpWebResponse httpWebResponse = 
      (HttpWebResponse)httpWebRequest.GetResponse()) 
    { 
    httpStatusCode = httpWebResponse.StatusCode; 

    if(httpStatusCode == HttpStatusCode.OK) 
    { 
     using(Stream responseStream = httpWebResponse.GetResponseStream()) 
     { 
     using(StreamReader responseReader = new StreamReader(responseStream)) 
     { 
      StringBuilder response = new StringBuilder(); 

      char[] read = new Char[256]; 
      int count = responseReader.Read(read, 0, 256); 

      while(count > 0) 
      { 
      response.Append(read, 0, count); 
      count = responseReader.Read(read, 0, 256); 
      } 
      responseReader.Close(); 
      return response.ToString(); 
     } 
     } 
    } 
    return null; 
    } 
}