5

I've integrated an option for users to pay via PayPal their online shopping on the web shop that I'm creating. The problem came up suddenly when I started to get this error:

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

And the code for the Http call is as following:

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

Can someone help me out with this? It worked fine a day ago, now its giving me this error?

perkes456
  • 1,163
  • 4
  • 25
  • 49

3 Answers3

9

Okay so if anyone is interested, I was able to fix the error by adding the following line before creating the web request (I was able to fix it by going down to Tls12 like this):

`ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12`;

Cheers :-)

Edit try this:

 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;
    }
perkes456
  • 1,163
  • 4
  • 25
  • 49
  • 1
    I am also facing same issue wih .net 4.0. Even after i add the ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; I face the same issue. Can you please help ? There is no Tls12 in 4.0 – sambit.albus Feb 09 '16 at 06:35
  • I have tried to implement your code but it gives same error. – sambit.albus Feb 10 '16 at 05:56
  • My problem was different than what is mentioned in question but your fix worked for me. In my case PayPal request are working fine in local machine but not working on production server. – Sandeep Aug 06 '16 at 04:59
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;
}
sambit.albus
  • 352
  • 1
  • 7
  • 26
0

After a number of hours wasted, it turned out to be the Tls protocol version.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Tony Pye
  • 31
  • 4