Cannot get request third time in a row - GetRequestStream() -

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Piotrekk

    #1

    Cannot get request third time in a row - GetRequestStream() -

    Hi

    I am uploading files to my ASP script one by one - from C#
    application. Each time little upload form is created with
    backgroundworke r in it. When I try to upload THIRD file the worker is
    stuck on getting requeststream.


    WebRequest req = WebRequest.Crea te(url);
    req.Method = "POST";

    //Headers
    req.ContentType = "multipart/form-data";
    req.ContentLeng th = fileSize;
    req.Headers.Add ("Name",file.Na me);
    req.Headers.Add ("Path", path);
    req.Headers.Add ("SessionID" , session);

    /// stuck here
    Stream stream = req.GetRequestS tream();

    When I debug it on the server during third time, the Page_Load event
    is not even called.
    Do you have any idea what could that be?

    Best Regards
    PK
  • Jon Skeet [C# MVP]

    #2
    Re: Cannot get request third time in a row - GetRequestStrea m() -

    Piotrekk <Piotr.Kolodzie j@gmail.comwrot e:
    I am uploading files to my ASP script one by one - from C#
    application. Each time little upload form is created with
    backgroundworke r in it. When I try to upload THIRD file the worker is
    stuck on getting requeststream.
    >
    >
    WebRequest req = WebRequest.Crea te(url);
    req.Method = "POST";
    >
    //Headers
    req.ContentType = "multipart/form-data";
    req.ContentLeng th = fileSize;
    req.Headers.Add ("Name",file.Na me);
    req.Headers.Add ("Path", path);
    req.Headers.Add ("SessionID" , session);
    >
    /// stuck here
    Stream stream = req.GetRequestS tream();
    >
    When I debug it on the server during third time, the Page_Load event
    is not even called.
    Do you have any idea what could that be?
    Yes. Chances are you're not closing the response, which means the
    connection to the server is left up, and you're only (by default)
    allowed two connections to any one server.

    Where you fetch the response, put it in a "using" statement and chances
    are it'll all start working with no issues.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon_skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Piotrekk

      #3
      Re: Cannot get request third time in a row - GetRequestStrea m() -

      Dear Jon. I'm not using response in my connection. Here is the entire
      backgroundworke r code:


      string url = String.Concat(t his.url);
      long fileSize = file.Length;

      WebRequest req = WebRequest.Crea te(url);
      req.Method = "POST";

      //Headers
      req.ContentType = "multipart/form-data";
      req.ContentLeng th = fileSize;
      req.Headers.Add ("Name",file.Na me);
      req.Headers.Add ("Path", path);
      req.Headers.Add ("SessionID" , session);

      Stream stream = req.GetRequestS tream();

      using (BinaryWriter writer = new BinaryWriter(st ream))
      {
      FileStream fs = new FileStream(file .FullName,
      FileMode.Open);

      using (BinaryReader reader = new BinaryReader(fs ))
      {
      int i = 0;
      long total = 0;

      byte[] buffer = new byte[32768];

      while (((i = reader.Read(buf fer,
      0,buffer.Length )) 0) && !Stop)
      {
      writer.Write(bu ffer,0,i);
      total += i;
      uploadWorker.Re portProgress((i nt)(total *
      100 / fileSize));
      }
      }
      }

      >
      Where you fetch the response, put it in a "using" statement and chances
      are it'll all start working with no issues.
      >
      --
      Jon Skeet - <sk...@pobox.co m>
      Web site:http://www.pobox.com/~skeet 
      Blog:http://www.msmvps.com/jon_skeet
      C# in Depth:http://csharpindepth.com

      Comment

      • Piotrekk

        #4
        Re: Cannot get request third time in a row - GetRequestStrea m() -

        req.GetResponse ().Close() did the work. Thank you.

        Comment

        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

          #5
          Re: Cannot get request third time in a row - GetRequestStrea m() -

          Piotrekk wrote:
          I'm not using response in my connection.
          If the server is working at all, there is always a response for each
          request. Even an error message (for example http 404) is a response.

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          Working...