none
The Uri string is too long error

    Question

  • Hi 

    When I'm trying to set a url into a WebRequest (C#) or into a Uri object, it throw : Invalid URI: The Uri string is too long.
    The url that I send includes a Xml with a content of a binary file in base64.

    How can I solve it?

    string url;

    Uri uri = new Uri(url);

    OR 

    WebRequest request = WebRequest.Create(url);

    Thanks!

    Monday, December 15, 2008 11:10 AM

Answers

  •  Hi Serg2008

    Thanks.

    Actually I used this article to solve it. The sample there fits perfectly to my needs.

    Thanks to you all.
    • Marked as answer by QApp Tuesday, December 16, 2008 7:45 AM
    Tuesday, December 16, 2008 7:44 AM

All replies

  •  UrU UriHi QApp
    All browser support a maximum length of a URL so it is a problem related to the browser and not the class Uri. 
    Try to use the POST Http web verb instead of GET (witch I assume that you are using).

    Have a look to this post and this too.

    Regards
    Hammadi

    Monday, December 15, 2008 4:20 PM
  • Thanks Hammadi,

    But it throws the error once I set the Uri:

    Uri uri = new Uri(url);

    Or here:

    WebRequest request = WebRequest.Create(url);

    The example here doesn't compile on WM6 anyway. (System.ServiceModel - not known on WM6)






    Monday, December 15, 2008 4:34 PM
  • It seems that length limit on URL is 0xfff0 = 65520 symbols (at least in .NET CF v2). If you want to pass more data then this you really better to use POST HTTP method instead of GET and write data to the output stream.

    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
    request.Method = "POST";
    //There are several more attributes of HttpWebRequest that you might want to set like ContentType, KeepAlive, ReadWriteTimeout and so on
    using(Stream requestStream = request.GetRequestStream())
    {
      requestStream.Write(buffer, 0, buffer.Length);
    }
    HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

    Here buffer is bytes array with data you want send
    • Edited by Serg2008 Monday, December 15, 2008 9:16 PM Add code
    Monday, December 15, 2008 9:04 PM
  •  Hi Serg2008

    Thanks.

    Actually I used this article to solve it. The sample there fits perfectly to my needs.

    Thanks to you all.
    • Marked as answer by QApp Tuesday, December 16, 2008 7:45 AM
    Tuesday, December 16, 2008 7:44 AM