0

I create a WebRequest in C#:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://myURL");

The URL is a request and may get big. Actually, depending on what the user does, I have no control over the size.

To prevent a case where the request is too long, I'd like to parse it.

Was is the maximum lenght of a string I can use to create a valid WebRequest?

Cher
  • 2,789
  • 10
  • 37
  • 64

4 Answers4

1

You'll be restricted by the server that receives the request, which will vary by server and configuration. As an example, Apache will DEFAULT to max length of about ~8100 characters, but that is only the default. A good rule of thumb is ~2000 characters, as that is what most browsers will max out at. As for the HTTP spec, it is unbounded, but you will find that any server can limit it as they wish. The Uri will limit it to about ~65k characters as noted in another answer.

SO question about the max length of Apache URL: What is apache's maximum url length?

SO question about browser limit: What is the maximum length of a URL in different browsers?

Community
  • 1
  • 1
JohnH
  • 81
  • 3
1

Following the RFC7230 there is no limitation to the URL size and suggest

that all HTTP senders and recipients support, at a minimum, request-line lengths of 8000 octets.

But there is physical limitation for WebRequest that consist in a maximum of 65519 chars

Community
  • 1
  • 1
Tinwor
  • 7,765
  • 6
  • 35
  • 56
1

The useful limit, as described in this answer, is about 2000 characters.

If you want to send many parameters through the URL, using a POST might be a solution more suited to your needs. This way the actual URL won't grow out of control, and the body of your request has a limit that is much, much higher.

Community
  • 1
  • 1
Jonathan M
  • 1,891
  • 13
  • 21
0

Internally that url string is parsed into Uri class instance - second overload of WebRequest.Create which accepts Uri instance is called. And you will get UriFormatException if:

The length of uriString exceeds 65519 characters or The length of the scheme specified in uriString exceeds 1023 characters.

Source: Uri Constructor (String) MSDN

Not all browsers support such long uri but if you are creating web requests programmaticaly, you will depend only on server limits.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459