2016-01-23 52 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;