none
Invalid URI: The Uri string is too long. RRS feed

  • Question

  • In my winform application , I used to make API calls using httpClient.

    These APIs were about inserting bulk data into database.

    When I send large json data as input parameter , I got below exception.

    Invalid URI: The Uri string is too long.

     HttpClient client = new HttpClient();
    
                    var request = new HttpRequestMessage(HttpMethod.Post, URI);
    
    
                    request.Headers.IfModifiedSince = new DateTimeOffset(DateTime.Now);
    
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    client.DefaultRequestHeaders
          .Accept
          .Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
                    var content1 = new FormUrlEncodedContent(content);
    
                    request.Content = content1;
                    request.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded; charset=utf-8");
    
                    //Debug.WriteLine(URI);
    
                    // Debug.WriteLine(await content1.ReadAsStringAsync());
                    var response = await client.SendAsync(request).ConfigureAwait(false);
                    //var response = await client.PostAsync(URI, content1).ConfigureAwait(false);
    
                    var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

    In above code , content = keyvaluepair<string,string>[]

    {

    new keyvaluepair<string,string>("apikey","xxx"),

    new keyvaluepair<string,string>("accesskey","yyyyy"),

    new keyvaluepair<string,string>("data","lengthyJSON"), //Very lengthy json

    }

    Can anyone help me to get rid of this issue?

    Thanks in advance for any quick reply.



    dinez


    • Edited by Dinez Tuesday, June 27, 2017 5:41 AM
    • Moved by Sheng Jiang 蒋晟 Friday, July 7, 2017 2:10 PM not winforms related
    Tuesday, June 27, 2017 5:30 AM

All replies

  • Hi Dinez,

    >>The Uri string is too long.

    With a post can include the content in the http message instead of the URI. A uri has a max length of 2083 characters. You could send it as JSON in the http message instead of the URI which is the recommended way to send larger chunks of data in an HttpPost/HttpPut.

    You can refer to the following similar link to solve your problem:

    HttpClient: The uri string is too long

    Or use MultipartFormDataContent  instead of FormUrlEncodedContent:

    Why doing POST throws an exception in MVC 4?

    Also some similar question:

    Invalid URI: The Uri string is too long. HttpClient

    The Uri string is too long error

    Hope this helps!

    Best Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 27, 2017 7:59 AM
  • Hi Dinez,

    Could you share us the definition of API?

    I suggest you try to serialize your object with Json, here is a simple code:

    API:

            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite == null)
                {
                    throw new ArgumentNullException("composite");
                }
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
    

    Object

        public class CompositeType
        {
            bool boolValue = true;
            string stringValue = "Hello ";
    
            [DataMember]
            public bool BoolValue
            {
                get { return boolValue; }
                set { boolValue = value; }
            }
    
            [DataMember]
            public string StringValue
            {
                get { return stringValue; }
                set { stringValue = value; }
            }
        }
    

    HttpClient

                HttpClient httpClient = new HttpClient();
                var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
                var response = httpClient.PostAsync(url, content).Result;
                var dynamicResult = await response.Content.ReadAsStringAsync();
    

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 10, 2017 2:58 AM