1

I am feeding XML Nodes from an xml document into a string that creates a URI string in the format "&Name=Value=...etc" for a HttpWebRequest. When I create the request the and once I reach somewhere between 7520-8981 (assuming its 8192 (2^13)) characters in my URI(absolute) the response is "Request-URI Too Long."

I have code that is now working but still getting "Request-URI Too Long." sometimes.

I need: 1. What is the max characters I can have? 2. Need function to split up the URI string into multiple web requests to accomplish my task. AKA if maxURIlength is reached - make new string and new request without dropping the current XML node.

I can statically list out all the Node Names and break them up manually, but the list is getting very long and tedious to write out. Not to mention if it ever changes, it would be a code change.

Also have tried doing separate web request for each parameter, but this takes wayyy to long to get through all of them.

//this is what I currently have started
internal static List<string> formatParameterString(string model)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(<xmlfile here>);

            XmlNodeList nodeList = xml.SelectNodes("/AcmDeviceParameterExport/ParameterList/Parameter");

            List<string> parameters = new List<string>(); //need to break up string - too long for Uri

            //string parameters = "";
            foreach (XmlNode xn in nodeList)
            {
                string Name = xn["Name"].InnerText;
                string Value = xn["Value"].InnerText;

                if (Value == "") { }

                else {  parameters.Add("&" + Name + "=" + Value);   }
        }

//this is code for the function I need can this be cleaner???
internal static void configureParams(string IPaddr, string model)
        {
            List<string> parameters = formatParameterString(model);
            string UrlHead = string.Format(@"http://{0}", IPaddr);
            string UrlPart = "";
            string UrlPartTemp = "";
            string UriComplete = "";

            foreach (string param in parameters)
            {
                //create temp uri for length test
                UrlPartTemp += param;
                UriComplete = UrlHead + UrlPartTemp;
                UriComplete = UriComplete.Replace("#", "%23"); //'#' chars become fragment when converting to Uri
                Uri uri = new Uri(UriComplete);

                if (uri.AbsoluteUri.Length >= 8192)
                {
                    UriComplete = UrlHead + UrlPart;    //use real UrlPart
                    UriComplete = UriComplete.Replace("#", "%23"); //'#' chars become fragment when converting to Uri
                    uri = new Uri(UriComplete);
                    response = CallWebService(uri.AbsoluteUri);
                    Console.WriteLine(response);

                    //clear variables
                    UrlPart = "";
                    UrlPartTemp = "";
                    UriComplete = "";
                }
                UrlPart += param;
            }
        }

I expect my code to check if its above the limit, stop concatenating Uri, send the request and then start new Uri without dropping the current parameter. How did I do?

Dillon Tucker
  • 55
  • 1
  • 8
  • 1
    Found one thing wrong: when I clear the UrlPartTemp I was not adding the dropped param to it so there was a simple arithmetic mismatch when i do the temp test. Fixed it by doing UrlPartTemp = param; //tack on dropped param to temp instead of UrlPartTemp = ""; – Dillon Tucker Aug 21 '19 at 21:10
  • 1
    Why don't you do a `POST` request instead of stuffing data through URL? – Vlad DX Aug 21 '19 at 21:18
  • what happen if there are two elements with the same name? why dont you just upload the whole xml? – Steve Aug 21 '19 at 21:33
  • Since there are no real restrictions on length of url (https://stackoverflow.com/questions/1185739/asp-net-mvc-url-routing-maximum-path-url-length) first part can't be answered here (you may want to talk to service owners so)… For the rest splitting your params into fixed size groups https://stackoverflow.com/questions/419019/split-list-into-sublists-with-linq may be easier – Alexei Levenkov Aug 21 '19 at 21:48
  • 1
    @VladimirSerykh I figured out how to stuff all the parameters in the body with POST and it appears I can do it in one request now - its always more simple than meets the eye. This changes my approach to this entire software now. Should be able to get rid of a ton of unneeded bs. Thankyou!!! – Dillon Tucker Aug 21 '19 at 22:55
  • You're allowed to include request data in the body of a GET request, you know. – Dour High Arch Aug 21 '19 at 23:23

0 Answers0