PayPal API的HTTP调用存在问题

5
我已经在我正在创建的网店中集成了一个选项,让用户可以通过PayPal支付他们的在线购物。但突然出现了一个问题,当我开始收到以下错误时:
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;
        }

有人能帮我解决这个问题吗?它一个多小时前还能正常工作,但现在却出现了错误?

3个回答

9

好的,如果有人感兴趣,我成功修复了错误,通过在创建 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;
    }

1
我也遇到了与.NET 4.0相同的问题。即使我添加了ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;,我仍然面临着同样的问题。你能帮忙吗?在4.0中没有Tls12。 - sambit.albus
我尝试实现你的代码,但是它仍然出现相同的错误。 - sambit.albus
我的问题与问题描述中的不同,但您的修复方法对我有效。在我的情况下,PayPal请求在本地机器上运行良好,但在生产服务器上无法工作。 - Sandeep

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;
}

嗨,我已经更新了我的答案,请复制并粘贴我的代码,如果它对你有用,请尝试一下。 - perkes456

0

经过数小时的浪费,最终发现问题出在 Tls 协议版本上。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接