1

I'm currently writing a simple app that performs a series of requests to the web server and I've encountered a strange... feature?

I don't need response stream of the request, but only status code. So, for each piece of my data I call my own "Send" method:

    public static int Send(string uri)
    {
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        try
        {
            request = (HttpWebRequest)WebRequest.Create(uri);
            response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK) return 0;
        }
        catch (Exception e)
        {
            if (request != null) request.Abort();
        }
        return -1;
    }

Works fine? Yes, unless I call this function at least twice. Second call of such a function in a row (with the same uri) will ALWAYS result in timeout.

Now, that's odd: if I add request.Abort(); when I return zero (here, when status code is 200) - everything ALWAYS works fine.

So my question is - why? Is it some kind of framework restriction, or maybe the some kind of anti-DOS protection on the particular server (unfortunately, the server is a black box for me)? Or maybe I just don't understand smth in how it all works?

millimoose
  • 39,073
  • 9
  • 82
  • 134
iehrlich
  • 3,572
  • 4
  • 34
  • 43
  • 2
    What happens if you call response.Close() before exiting? – Japple Jun 14 '12 at 21:54
  • Japple, suddenly, it works fine again. And it seems to be much more logical then aborting successful request. But still - why? – iehrlich Jun 14 '12 at 21:56
  • 1
    It looks like Cedric Rup has it below. It seems related to this as well http://stackoverflow.com/questions/4033159/c-sharp-httpwebrequest-times-out-after-two-server-500-errors – Japple Jun 14 '12 at 22:01
  • 1
    If all you want is the status code, try setting request.Method = "HEAD". That way, you won't have to transfer the whole page content. – Steve Konves Jun 14 '12 at 22:09

2 Answers2

2

Try to dispose of the web response, you may leak some resources

public static int Send(string uri)
    {
        HttpWebRequest request = null;
        try
        {
            request = (HttpWebRequest)WebRequest.Create(uri);
            using (var response = (HttpWebResponse)request.GetResponse())
            {
               if (response.StatusCode == HttpStatusCode.OK) return 0;
            }
        }
        catch (Exception e)
        {
            if (request != null) request.Abort();
        }
        return -1;
    }

There is also a default number of connections (2 I think, but you can configure this) you can make to a domain simultaneously, please see this SO question. You're probably hitting this limit with your unclosed responses.

Community
  • 1
  • 1
Cédric Rup
  • 15,468
  • 3
  • 39
  • 30
  • So please tell me what resources may I leak? And I don't perform any simultaneous connections. – iehrlich Jun 14 '12 at 22:00
  • If you don't close your response, the web request remains open. When you call the method a second time, you have two opened connections simultaneously... – Cédric Rup Jun 14 '12 at 22:02
-1

First of all I'd make a series of changes in order to get to the root of this:

  • take out that try..catch{} (you're likely swallowing an exception)
  • return a boolean instead of a number

You should then get your exception information you need.

Also you should be using "HEAD" as your method as you only want the status code:

request.Method = "HEAD";

read the difference here.

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • As I said, the function always results in timeout. And why I said it is because I processed the exceptions using this try..catch{} block. And it is still here because I deleted all irrelevant code. And it is definitely not an answer to my question. – iehrlich Jun 14 '12 at 21:51