0

I have an application on windows mobile platform cf3.5. This communicates with a web server and they do it nicely, requests get correct replies. However, on every request it throws a System.UriFormatException in visual studio output window. Uris look like this:

http://api/Information/TagKey/4952b9ff-af55-4219-95b8-1ac57a70b7a8/L0011421

How can I know what is wrong with these uris? Should I change '-' chars in the guid fields or what?

I build the uri by this method, where uri is the base address of the server and dictionary contains the parameters:

private static string buildUri(string uri, Dictionary<string, string> data)
    {
        StringBuilder _uri = new StringBuilder(uri);
        foreach(KeyValuePair<string, string> item in data) 
        {
            _uri.Append("/" + item.Value);
        }
        return _uri.ToString();
    }

This is used by this createRequest method:

private static HttpWebRequest createRequest(string uri, HttpMethods method, Dictionary<string, string> data, Dictionary<string, object> postdata)
    {
        string fullUri = buildUri(uri, data);
        Classes.Log.Write("/// "+fullUri);
        WebRequest request = HttpWebRequest.Create(fullUri);
        request.Method = Enum.ToObject(typeof(HttpMethods), method).ToString();
        request.ContentType = "application/json; charset=utf-8";
        ((HttpWebRequest)request).Accept = "application/json; charset=utf-8";
        request.ContentLength = 0;
        if (method != HttpMethods.GET && method != HttpMethods.DELETE && postdata != null)
        {
            Encoding encoding = Encoding.UTF8;
            string postData = buildPostData(postdata, "application/json");
            byte[] byteArray = encoding.GetBytes(postData);
            request.ContentLength = byteArray.Length;
            if (byteArray != null && byteArray.Length > 0)
            {
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(byteArray, 0, (int)request.ContentLength);
                }
            }
        }
        return (HttpWebRequest)request;
    }

It throws the exception at

WebResponse response = request.GetResponse();

that is in an other method.

I have generated an Uri object first by the string and it doesn't throw any exceptions.

Perrier
  • 2,753
  • 5
  • 33
  • 53
  • can you show the code where you create the uri and then do the request? Some of the parsers (I assume the uri too) and throw internal exceptions during parsing the string. And yes, that is bad behavior in regards of resource usage. – josef Sep 02 '14 at 03:33
  • I've added the correspondig methods. Thanks for your help. – Perrier Sep 02 '14 at 07:31
  • In the code createRequest() you build an URL string and not an URI object. This code is not throwing an exception? OK, but you get an exception in another method when calling "WebResponse response = request.GetResponse();". Can you show this code too? And, as you say "I have generated an Uri object first by the string and it doesn't throw any exceptions.", why do you not use an URI object or at least let the URI object create the correctly escaped URL string for you and then use the string (if you can not use the URI directly)? – josef Sep 03 '14 at 03:14

0 Answers0