Making multiple httpwebrequest from a client.

I have a desktop client which connects to a http server.
Now when i am downloading a file from the server and in the meantime if i make any other httpwebrequest to it
the connection just times out and i'm not able to get any data. Also the filedownload is interuppted without an exception.

I have tried changing the webrequest.connectiongroupname but to no use.

Am i missing something in it?

Skot
LVL 1
sourabhkothariAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

2266180Commented:
yes, you must use different webrequest for every request. a webrequest instance is for one request only. it cannot handle multiple requests in the same time.

a good aproach for your situation is to make a separate class, that will spawn a new thread, instantiate a new webrequest and get the resource.
sourabhkothariAuthor Commented:
Sorry for not clarifying it earlier.
Yes i am using different threads and different httpwebreqeust instanecs for each request but if a file download is going on from the server and i make another httpwebrequest than it just times out and the filedownload is also interrupted.
The client connects to the server many a times, when there is a file download going on after a few webrequest the requests start timing out and the filedownload stream disconnects.

Hope it helps.
2266180Commented:
so you have a request ongoing with a file download, and then you make another few requests that succeed, and after a few such requests, anotehr one is made that will cancel the ongoing download and also timeout. is this it?

if so, is the number of requests made after the file download start always the same? is it always happening for the same same request, no matter what and how maney requests succed after the file download begin and the time out occures?

can you post some code that reproduces this issue? just create a nother prject and cut out unnecessary code from your existing project to make a small app that reproduces this. just so that I don't waste my time writeing it all. if it's too complicated, then just let me know so that I can start codeing ;)

cheers
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

sourabhkothariAuthor Commented:
The number of requests after the file download vary. Everytime some arbit no of requests complete and than times out.
Also, if there is no file download going on and i make other request they all complete successfully.

I have tried to download the file using webclient and also by normal httpwebrequest but the result is same.

I make 3 kinds or request to the server:
1. File
2. A small image of approx 3-4 kb.
3. XML

If i do no. 2 and no. 3 in any order any number of times there is no problem.
Only when no.1 is there and i request no.2 or no.3 than there is a problem.
2266180Commented:
Hi,

this works just fine for me. I am sure that you are doing something wrong in your code. Are you absolutly posiztive that you are using different instances?
It would probably be best if you will post a sample code that reproduces the issue.

Here is the code I used:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net;
using System.IO;

namespace WindowsApplication7
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 266);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.Load += new System.EventHandler(this.Form1_Load);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            public class Downloader
            {
                  private string Url;
                  public Thread t = null;
                  public bool ok = false;
                  
                  public Downloader(string url)
                  {
                        Url = url;
                        t = new Thread(new ThreadStart(Get));
                        t.Start();
                  }

                  public void Get()
                  {
                        try
                        {
                              HttpWebRequest r = (HttpWebRequest)HttpWebRequest.Create(Url);
                              HttpWebResponse resp = (HttpWebResponse)r.GetResponse();
                              Stream s = resp.GetResponseStream();
                              while (s.ReadByte() != -1)
                              {
                                    // do nothing; or save the byte if you want to save the file :)
                              }
                              resp.Close();
                              ok = true;
                        }
                        catch
                        {
                        }
                  }
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
                  Downloader d1 = new Downloader("http://www.linuxiso.org/download.php/591/KNOPPIX_V3.7-2004-12-08-DE.iso");// just a big file :)
                  int max = 25;
                  Downloader[] d = new Downloader[max];
                  string[] s = {"http://www.noble.org/imagegallery/images/indiangrassCrop.jpg",
                                    "http://www.noble.org/imagegallery/IndianBlanketForHome.jpg",
                                    "http://www.noble.org/imagegallery/images/redbudCrop.jpg",
                                    "http://www.noble.org/Storefront/AgStore/StoreFront/Images/NF-FO-04-01.jpg"
                  };
                  for (int i=0; i<max; i++)
                        d[i] = new Downloader(s[i % 4]);
                  for (int i=0; i<max; i++)
                        d[i].t.Join();
                  bool ok = true;
                  for (int i=0; i<max; i++)
                        ok = ok && d[i].ok;
                  if (ok)
                        System.Windows.Forms.MessageBox.Show("OK");
                  else
                        System.Windows.Forms.MessageBox.Show("NOT OK");
            }
      }
}


it showed "OK" :)

I have to go now since it's new years eve in a few hours. I'll get back to you sometime tomorrow.

cheers and have a  happy new year.
2266180Commented:
just wanted to let you know that I'm back and watching the question. (let me know if my previous demo does not cover your situation)

cheers
sourabhkothariAuthor Commented:
Sorry for not replying all this while...I too was out of station.

Yes, i am doing same as u suggested with a slight difference. As in, in all the other threads i'm downloading small images in a loop.
( Typically 20 small images).
Also, i want to ask if there is some bandwidth limitation. Like, as i download the big file it sucks up all the availabel bandwidth and now when i make new requests they do not get enough bandwidth and time out.
( Typically the errors given are of time out and sometimes it is 'the underlying connection was closed . the request was cancelled'.)
2266180Commented:
from what I know, all issues related to bandwitdh management are solved by the OS. The again I might be wrong since I don't know much on how things are implemented in .NET
but I would say not, going after my demo for the simple reason that it worked for me :)
so, the big file started downloading.and then a lot of small files started downloading.
you could of cousre modify you code to also show speeds (how many bytes read in one second), which in case of the big file should drop when the first small images start downloading.
you should also take into consideration timeouts on the server from where you are downloading from. There are servers that limit the number of concurrent downloads from one client.
The latter might be the case since I modified the "Get" method as shown below and I still got an "OK"
                  public void Get()
                  {
                        try
                        {
                              for (int i=0; i<10; i++)
                              {
                                    HttpWebRequest r = (HttpWebRequest)HttpWebRequest.Create(Url);
                                    HttpWebResponse resp = (HttpWebResponse)r.GetResponse();
                                    Stream s = resp.GetResponseStream();
                                    while (s.ReadByte() != -1)
                                    {
                                          // do nothing; or save the byte if you want to save the file :)
                                    }
                                    resp.Close();
                              }
                              ok = true;
                        }
                        catch
                        {
                        }
                  }
            }

I forgot to ask: what versino of the framework are you using? I'm using version 1.1. Maybe this is the issue?

One test would be to let the big file download, and then download a lot of the small images, but one after another. So there will be only 2 trheads: 1 for the big file and one for the small files. If this test fails, then there is a high posibility that the issue is on your code side, if it doesn't fail, then it is probably on the server side, but could be on your code side as well.

regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.