Is there a limit on the size of a http argument va

2019-02-26 10:59发布

问题:

I am testing out a few different public RESTful APIs each differing in http argument value name, but in concept all work similarly. However none of the companies are connected so it must be something on my end.

Within .NET I get the following error when trying to obtain the HttpWebResponse:

Message: "The server committed a protocol violation. Section=ResponseStatusLine"
Status: ServerProtocolViolation {11}

Using Fiddler, I am finding that the headers coming back are non-existent and this is the cause of the issue (not even present and improperly formatted):

HTTP/1.0 200 This buggy server did not return headers

The parameter I am sending in is a lengthy text news article. I have stripped ALL puntuation, characters, etc just to make sure nothing wans interferring. I build the text out using a StringBuilder and then create the URI like below:

Dim sb As New StringBuilder
sb.Append("&apiKey=" + HttpUtility.UrlEncode("123456789"))
sb.Append("&inputText=" + HttpUtility.UrlEncode(Me.MyTestText))
Dim ApiHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(New Uri(BaseRequestURL + sb.ToString.Trim())), HttpWebRequest)
Dim ApiHttpWebResponse As HttpWebResponse = CType(ApiHttpWebRequest.GetResponse(), HttpWebResponse)

This code DOES work with most text I send it. The interesting thing I am finding is that if I shorten the text I am sending then it works! I can't figure out exactly the threashold or breaking point. The stringbuilders values expand after the HttpUtility.UrlEncode function is run against it. However the text in size is only about 5-8 kilobytes in size and well below any size limitations set forth by the 'httpWebRequest' .config settings as laid out in the MSDN or by that of any of the APIs I am using (for example 1 limit is 150 kilobytes of text). I even tried setting all of those values to "-1" in the web.config which means: "A value of -1 indicates that no size limit will be imposed on the response headers."

Is there a limit on the size of the http argument value or anything else here I am missing that would cause this? Thanks!

回答1:

See this SO: What is the limit on QueryString / GET / URL parameters



回答2:

Well as typical when I post a question, the problem gets scared, and presents the solution ;)

Thank you for the previous link, but since I wasn't doing this directly in a browser, those limitations were not the issue in my case.

I was using the HttpWebRequest's defualt 'Method' value which is "GET" and I should be using "POST" for a few reasons, including the conquering the length of the request issue. So adding the following line of code after creation of the Request object, fixed the issue:

ApiHttpWebRequest.Method = "POST"

This link (old but content still holds) was also helpful:

What is the limit on QueryString / GET / URL parameters?:
http://classicasp.aspfaq.com/forms/what-is-the-limit-on-querystring/get/url-parameters.html

...and a good description from TechNet:

"The GET method, which was used in the example earlier, appends name/value pairs to the URL. Unfortunately, the length of a URL is limited, so this method only works if there are only a few parameters. The URL could be truncated if the form uses a large number of parameters, or if the parameters contain large amounts of data. Also, parameters passed on the URL are visible in the address field of the browsernot the best place for a password to be displayed.

The alternative to the GET method is the POST method. This method packages the name/value pairs inside the body of the HTTP request, which makes for a cleaner URL and imposes no size limitations on the forms output. It is also more secure."