2016-01-23 77 views
4

我已經集成了一個選項供用戶通過PayPal在我正在創建的網上商店上進行網上購物。問題就來了時,突然我開始收到此錯誤:PayPal API的問題HTTP呼叫

You must write ContentLength bytes to the request stream before calling [Begin]GetResponse. 

和代碼爲HTTP調用如下:

public string HttpCall(string NvpRequest) 
     { 
      string url = pEndPointURL; 

      string strPost = NvpRequest + "&" + buildCredentialsNVPString(); 
      strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode); 

      HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url); 
      objRequest.Timeout = Timeout; 
      objRequest.Method = "POST"; 
      objRequest.ContentLength = strPost.Length; 

      try 
      { 
       using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream())) 
       { 
        myWriter.Write(strPost.ToString()); 
       } 
      } 
      catch (Exception e) 
      { 

      } 

      //Retrieve the Response returned from the NVP API call to PayPal. 
      HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); // this is the line where the exception occurs... 
      string result; 
      using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) 
      { 
       result = sr.ReadToEnd(); 
      } 

      return result; 
     } 

有人能幫助我嗎?它在一天前運行良好,現在它給了我這個錯誤?

+0

真的嗎?沒人知道? :/ – perkes456

回答

8

好了,所以如果有人有興趣,我可以通過創建一個web請求之前添加以下行來修正錯誤(我能夠延續到Tls12這樣來解決它):

`ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12`; 

乾杯:-)

編輯試試這個:

public string HttpCall(string NvpRequest) 
    { 
     string url = pEndPointURL; 

     string strPost = NvpRequest + "&" + buildCredentialsNVPString(); 
     strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode); 
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 
     // Try using Tls11 if it doesnt works for you with Tls 
     HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url); 
     objRequest.Timeout = Timeout; 
     objRequest.Method = WebRequestMethods.Http.Post; 
     objRequest.ContentLength = strPost.Length; 
     try 
     { 

      using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream())) 
      { 
       myWriter.Write(strPost.ToString()); 
      } 
     } 
     catch (Exception e) 
     { 

     } 

     //Retrieve the Response returned from the NVP API call to PayPal. 
     HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); 
     string result; 
     using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) 
     { 
      result = sr.ReadToEnd(); 
     } 

     return result; 
    } 
+0

我也面臨着與.net 4.0相同的問題。即使我添加了ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;我面臨同樣的問題。你能幫忙嗎?在4.0中沒有Tls12 –

+1

你可以在這裏粘貼你的代碼,以便我可以看到我們在這裏處理的是什麼...... – perkes456

+0

我試圖實現你的代碼,但它給出了相同的錯誤。 –

0
public string HttpCall(string NvpRequest) //CallNvpServer 
{ 
    string url = pendpointurl; 

    //To Add the credentials from the profile 
    string strPost = NvpRequest + "&" + buildCredentialsNVPString(); 
    strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode); 

    HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url); 
    objRequest.Timeout = Timeout; 
    objRequest.Method = "POST"; 
    objRequest.ContentLength = strPost.Length; 

    try 
    { 
     using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream())) 
     { 
      myWriter.Write(strPost); 
     } 
    } 
    catch (Exception e) 
    { 
     /* 
     if (log.IsFatalEnabled) 
     { 
      log.Fatal(e.Message, this); 
     }*/ 
    } 

    //Retrieve the Response returned from the NVP API call to PayPal 


    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); 
    string result; 
    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) 
    { 
     result = sr.ReadToEnd(); 
    } 

    //Logging the response of the transaction 
    /* if (log.IsInfoEnabled) 
    { 
     log.Info("Result :" + 
        " Elapsed Time : " + (DateTime.Now - startDate).Milliseconds + " ms" + 
        result); 
    } 
    */ 
    return result; 
} 
+0

嗨,我已經更新了我的答案,複製並粘貼我的代碼,如果它適合您,請嘗試一下。 – perkes456

+0

同樣的錯誤。 .net 4.0中也不存在Tls11 –

0

浪費,它噸數小時後呃出來是Tls協議版本。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;