Razor with papyal. RRS feed

  • Question

  • User1573990591 posted

    Hey guys, i've seen example and i've tried and use it..

    I cant spot the problem, tell me if u guys can?

    this is my code:

    @using System.Collections.Generic
    @using System.Text
    @using System.Web
    @using System.Web.UI
    @using System.Web.UI.HtmlControls
    @using System.Web.UI.WebControls
    @using System.ComponentModel
        Page.Title = "Checkout | SSSSS";
        string postUrl = "https://www.paypal.com/cgi-bin/webscr";
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postUrl);
        //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 = System.Text.Encoding.UTF8.GetString(param);
        string ipnPost = strRequest;
        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(), 
        StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
        string strResponse = streamIn.ReadToEnd();
        /*/ logging ipn messages... be sure that you give write
        // permission to process executing this code
        string logPathDir = ResolveUrl("Messages");
        string logPath = string.Format("{0}\\{1}.txt", 
                         Server.MapPath(logPathDir), DateTime.Now.Ticks);
        File.WriteAllText(logPath, ipnPost);
    @if (strResponse == "VERIFIED")
        /*---------------- WILL DO OTHER CHECKS LATER    ------------------*/
        //check the payment_status is Completed
        <p>status is verified</p>
        //check that txn_id has not been previously processed
        //check that receiver_email is your Primary PayPal email
        //check that payment_amount/payment_currency are correct
        //process payment
    else if (strResponse == "INVALID")
        //log for manual investigation
        <p>status is invalid.</p>
        //log response/ipn data for manual investigation
        <p>status is invalid.</p>

    the error is the following:

    Error is this:
    Exception Details: System.IO.IOException: Cannot close stream until all bytes are written.
    and its pointing on this line:

    Thursday, August 7, 2014 3:15 AM


  • User895691971 posted

    The problem is really very simple. In Stream class, you have the ability to use the method called Close that would close the connection to the current Network resource that you're using. It is helpfull as it would not let leakage of resources from the users' computer. 

    So while you use the method


    It is a good approach and is recommended to, that you close your connections to all resources once you're done working with them. Whereas here, you're closing it before that. That is where exception comes up. 

    Cannot close stream until all bytes are written.

    This error tells you that you're closing the connection to the connection too early than expected to be closed. If you do so, you might loose content on the file, you might get other errors too. That is why it keeps telling you about this.

    What you can do about it, to place the closing for the resources at the very end of the codes. That's what I do, I place all the resource closing codes at the very end of the code block so that even if I have to use them later I can access them inside that code block. Further more, I try to look for a built-in close method, since some of the Stream methods include it and they flush, dispose and close the method themself and they take care of themself, themself. It is a good approach to first look for the File class at the MSDN. 


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 7, 2014 7:08 PM