Locksleyu
Locksleyu

Reputation: 5375

Maximum URL length with System.Net.HttpListener (C#)

I have written a simple server using System.Net.HttpListener in C# via the following code:

Listener = new HttpListener();
Listener.Prefixes.Add(Prefix);
Listener.Start();
Listener.BeginGetContext(GetContextCallback, null);

I have noticed that my server returns a 400 (Bad Request) HTTP response whenever the URL I send (in the HTTP GET) is over ~255 characters. I have done some research on this and it seems that there is no standard, though most browsers and servers seem to handle at least ~2000 characters.

The funny thing is my GetContextCallback() is never even being called when it fails this way.

I really would like to figure out how to configure this to support larger than 255 characters, does anyone know if is possible?

Thanks!

Update: I discovered this limitation doesn't appear to be on the entire URL, but rather on an individual directory name. I'm guessing this has something to do with MAX_PATH, but I know for a fact other servers can handle this, and I have seen actual links "in the wild" that have directory names that are 300+ characters (Google in particular for links like /extern_js/f/........).

Update: I am testing my server with telnet, using the following GET request:

GET /013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789013456789/test.html
Host: 10.15.38.80:8008
Accept: */*

Upvotes: 4

Views: 2138

Answers (1)

Jan Sommer
Jan Sommer

Reputation: 3808

There doesn't seem to be any other way but setting a value in the registry.

Go to HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\HTTP\Parameters and create a DWORD (32 bit) parameter named UrlSegmentMaxLength. Set the value to 0 and you won't be bothered with long url issues anymore.

You have to restart before the change is applied.

Source: http://support.microsoft.com/kb/820129

Upvotes: 5

Related Questions