Search code examples
c#httppostvisual-studio-2013fiddler

Poor HTTP request performance unless using Fiddler


Edit: after talking it over with a couple IT guys, I've realized it's only the POLL requests that are having issues. I'm fetching the images via GET requests that go through quickly and as expected, whether or not the POLL messages are having issues.

I'm working on a client to interface with an IP camera in C#.

It's all working dandy except that I can get really poor http request performance when I'm not using Fiddler (a web traffic inspection proxy).

I'm using an httpclient to send my requests, this is my code that actually initiates the poll request:

public async Task<bool> SetPoll(int whichpreset)
    {
        string action = "set";
        string resource = presetnames[whichpreset];
        string value = presetvalues[whichpreset];
        int requestlen = 24 + action.Length + resource.Length + value.Length;

        var request = new HttpRequestMessage
        {
            RequestUri = new Uri("http://" + ipadd + "/res.php"),
            Method = HttpMethod.Post,

            Content = new FormUrlEncodedContent(new[]{
                new KeyValuePair<string,string>("action",action),
                new KeyValuePair<string,string>("resource",resource),
                new KeyValuePair<string,string>("value",value)
            }),
            Version = new System.Version("1.1"),

        };
        HttpResponseMessage mess = await client.SendAsync(request);
        if (mess.IsSuccessStatusCode)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

When Fiddler is up, all my http requests go through quickly, and without a hitch (I'm making about 20 post requests upon connecting). Without it, they only go through as expected ~1/5 of the time, and the rest of the time they're never completed, which is a big issue. Additionally, the initial connection request often takes 1+ minutes when not using Fiddler, and consistently only takes a few seconds when I am, so it doesn't seem to be a timing issue of sending requests too soon after connecting.

This leads me to think that the request, as written, is fairly poorly behaved, and perhaps Fiddler's requests behave better. I'm a newbie to HTTP, so I'm not sure exactly why this would be. My questions:

  1. does Fiddler modify HTTP requests (E.G. different headers, etc.) as they are sent to the server?
  2. even if it doesn't modify the requests, are Fiddler's requests in some way better behaved than I'd be getting out of .net 4.0 in C# in VS2013?
  3. is there a way to improve the behavior of my requests to emulate whatever Fiddler is doing? Ideally while still working within the stock HTTP namespace, but I'm open to using others if necessary.

I'll happily furnish more code if helpful (though tomorrow).


Solution

  • Inserting

    await Task.Delay(50);
    

    between all requests fixed the problem (I haven't yet tested at different delays). Because Fiddler smoothed the problem out, I suspect it's an issue the camera has with requests sent in too quick of a succession, and fiddler sent them at a more tolerable rate. Because it's an async await, there is no noticeable performance impact, other than it taking a little while to get through all ~20 (30 now) requests through on startup, which is not an issue for my app.