2012-01-03 310 views
1

我試圖設置PayPal Ipn,它在某些訂單驗證上失敗。我發現它失敗如果用戶名稱有一些非標準字母,如&last_name=Montalvo Agüera 我是否需要更改編碼?PayPal驗證失敗,無效

var request = "cmd=_notify-validate&......."; 

const string strLive = "https://www.paypal.com/cgi-bin/webscr"; 
    var req = (HttpWebRequest)WebRequest.Create(strLive); 
      //Set values for the request back 
      req.Method = "POST"; 
      req.ContentType = "application/x-www-form-urlencoded"; 

      req.ContentLength = request.Length; 

      var streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII); 
      streamOut.Write(request); 
      streamOut.Close(); 
      var streamIn = new StreamReader(req.GetResponse().GetResponseStream()); 
      var strResponse = streamIn.ReadToEnd(); 
      streamIn.Close(); 

      Response.Write(strResponse); 

回答

0

您需要對參數進行網址編碼。這就是req.ContentType = "application/x-www-form-urlencoded";的含義。這是你向HTTP服務器作出承諾(在這種情況下,貝寶的www.paypal.com)。承諾是您發送的任何數據都將進行網址編碼。這意味着你必須轉義任何特殊字符。這包括字符如?&%以及字符如ü

進行URL編碼的名稱,你需要建立與URL編碼名稱的請求:

string request = "cmd=_notify-validate&......."; // don't include "last_name=" 
string name = "Montalvo Agüera"; 
request += "last_name=" + Server.UrlEncode(name); 
+0

不,這不起作用。 – Tomas 2012-01-03 09:07:13

+0

仍然無效。兩天試圖解決問題沒有取得任何成功。我從「即時付款通知(IPN)詳細信息」頁面獲得IPN消息,其中一些失敗。我甚至認爲只是刪除驗證。 – Tomas 2012-01-03 09:17:26

+0

你能編輯這個問題嗎?它有兩個示例請求 - 一個可以工作,一個不可以?可能還有其他問題。 – scraimer 2012-01-03 10:48:15

0

,您可以嘗試這樣的:

string strLive = "https://www.paypal.com/cgi-bin/webscr"; 
     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLive); 

     //Set values for the request back 
     req.Method = "POST"; 
     req.ContentType = "application/x-www-form-urlencoded"; 
     byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength); 
     string strRequest = Encoding.ASCII.GetString(param); 
     strRequest += "&cmd=_notify-validate"; 
     req.ContentLength = strRequest.Length; 

     //for proxy 
     //WebProxy proxy = new WebProxy(new Uri("http://url:port#")); 
     //req.Proxy = proxy; 

     //Send the request to PayPal and get the response 
     StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII); 

如果仍然沒有按」 t工作嘗試將編碼更改爲UTF8

+0

我得到同樣的問題。某些訂單的狀態無效。 – Tomas 2012-01-03 09:08:13

+0

如果我將其更改爲UTF8,則會出現錯誤「在寫入所有字節之前無法關閉流。」 – Tomas 2012-01-03 09:18:47

+0

我認爲你應該聯繫貝寶 – kleinohad 2012-01-03 09:36:48