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?