C# StreamWriter Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SpecialKay
    New Member
    • Mar 2008
    • 109

    C# StreamWriter Problem

    i got a nice problem here:
    my page recieves a request, it then sends a request back to the other server with all of the same params as the orignal requess, and waits for a responce the responce should be "Valid" or "Invalid"
    and then my pages sends back the orignal response witch is discarded.

    When i run on local.host everything works perfectly.
    I upload my files to the server, and run the page and it crashes at this line of code:

    [code=cpp]
    StreamWriter streamOut = new StreamWriter(re q.GetRequestStr eam(), System.Text.Enc oding.ASCII);
    [/code]



    the full function is:

    [code=cpp]
    protected void Page_Load(objec t sender, EventArgs e)
    {
    LogAMessage.log AMessage("1");
    //Post back to either sandbox or live
    string strSandbox = "https://www.sandbox.pay pal.com/cgi-bin/webscr";
    //string strLive = "https://www.paypal.com/cgi-bin/webscr";
    HttpWebRequest req = (HttpWebRequest )WebRequest.Cre ate(strSandbox) ;
    LogAMessage.log AMessage("2");
    //Set values for the request back
    req.Method = "POST";
    LogAMessage.log AMessage("3");
    req.ContentType = "applicatio n/x-www-form-urlencoded";
    LogAMessage.log AMessage("4");
    byte[] param = Request.BinaryR ead(HttpContext .Current.Reques t.ContentLength );
    LogAMessage.log AMessage("5");
    string strRequest = Encoding.ASCII. GetString(param );
    LogAMessage.log AMessage("6");
    strRequest += "&cmd=_noti fy-validate";
    LogAMessage.log AMessage("7");
    req.ContentLeng th = strRequest.Leng th;
    LogAMessage.log AMessage("8");

    //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(re q.GetRequestStr eam(), System.Text.Enc oding.ASCII);
    LogAMessage.log AMessage("9");
    streamOut.Write (strRequest);
    LogAMessage.log AMessage("10");
    streamOut.Close ();
    LogAMessage.log AMessage("11");
    StreamReader streamIn = new StreamReader(re q.GetResponse() .GetResponseStr eam());
    LogAMessage.log AMessage("12");
    string strResponse = streamIn.ReadTo End();
    LogAMessage.log AMessage("13");
    streamIn.Close( );
    LogAMessage.log AMessage("14");

    if (strResponse == "VERIFIED")
    {
    LogAMessage.log AMessage("IPN is Verified");
    }
    else if (strResponse == "INVALID")
    {
    LogAMessage.log AMessage("IPN is INVALID");
    }
    else
    {
    LogAMessage.log AMessage("IPN BROKEN");
    }
    }

    [/code]

    the LogAMessage.log AMessage() function is a simple function that writes a message to a text file. My text file gets up to number 8 and then nothing.

    Thanks for your help.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I think when you call the RequestStream it does somesort of "pre-call" to the server....maybe ?

    Comment

    • SpecialKay
      New Member
      • Mar 2008
      • 109

      #3
      by "pre-call" you mean?

      waiting for reply

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Like when you say get request stream, it sends a partial request to the server "POST /somepage.asp HTTP/1.1" and then when you finish with the stream it sends the rest?
        "Content-Length: 10

        asdfghjklq"

        Comment

        • SpecialKay
          New Member
          • Mar 2008
          • 109

          #5
          doesnt sound right,
          do you have any suggestion on how i can fix it?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Hmm, I'm starting to doubt my original guess at the problem.
            What exception is thrown?

            Comment

            • SpecialKay
              New Member
              • Mar 2008
              • 109

              #7
              Absolutly NOTHING, code just stops. No exceptions at all, and it doesnt not go into the Finally()

              Comment

              • SpecialKay
                New Member
                • Mar 2008
                • 109

                #8
                OOps sorry, i have now put the code into a try()catch()fin ally() and i have tried to catch all possible exceptions with no luck, and the finally doesnt not get called either. The code just stops.

                My theory for now is simply that my server is blocking the out going request.

                Comment

                • SpecialKay
                  New Member
                  • Mar 2008
                  • 109

                  #9
                  Update to problem
                  execption thrown = Unable to connect to remote server

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Ah ha, well that's where you need to fix, why it cannot connect to server.
                    Is the URL being passed to it correct?

                    Comment

                    • SpecialKay
                      New Member
                      • Mar 2008
                      • 109

                      #11
                      Answer: Firewall problem.

                      Comment

                      Working...