1

A simple request to the server to get a file is giving some issues, Here's the url,

https://dc2-vault.myvzw.com/dv/api/user/c107a6db69104a10bc247a28fb81131e/search?query=contentType:audio/* AND (genre like 'RoyalJatt.Com )')&sort=name+asc&start=1&count=2147483647

then i do this,

url = Uri.EscapeUriString(url);

i get,

https://dc2-vault.myvzw.com/dv/api/user/c107a6db69104a10bc247a28fb81131e/search?query=contentType:audio/*%20AND%20(genre%20like%20'RoyalJatt.Com%20)')&sort=name+asc&start=1&count=2147483647

The problem here is this,

the query looks for a file with Genre RoyalJatt.Com%20) which should have been RoyalJatt.Com%20%29

For some reason, it is omitting the ")"

What can i do to fix this ?

golldy
  • 1,279
  • 1
  • 15
  • 31

2 Answers2

1

Ok, so it seems that encoding data for use in a url is not the same as encoding a url!

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Model_Library
{
    [TestClass]
    public class test
    {
        [TestMethod]
        public void test2()
        {
            string url = @"https://dc2-vault.myvzw.com/dv/api/user/c107a6db69104a10bc247a28fb81131e/";
            string data = @"search?query=" + Uri.EscapeDataString(@"contentType:audio/* AND (genre like 'RoyalJatt.Com )')") + @"&sort=name+asc&start=1&count=2147483647";
            string enc = Uri.EscapeUriString(url) + data;

            Console.Write(enc);
        }
    }
}
Ewan
  • 1,261
  • 1
  • 14
  • 25
  • if i change "RoyalJatt.Com )" to "RoyalJatt.Com" this does not create an issue. – golldy Apr 13 '15 at 21:23
  • sorry don't understand – Ewan Apr 13 '15 at 21:25
  • Please don't reinvent the wheel... There are already existing classes like Uri, UriBuilder and HttpUtility.ParseQueryString to deal with creation of urls and query string.s – Alexei Levenkov Apr 13 '15 at 21:26
  • 1
    I guess there's "whats the best way of building a url in c#?" and "Why doesn't Uri.EscapeUriString() encode my url as expected" – Ewan Apr 13 '15 at 21:31
  • @AlexeiLevenkov sorry to disagree... but Classes like HttpUtility does not exist in the Windows Phone applications or even windows applications. They are web based. I do admit that i have not added the necessary information that i am working on a phone app. Anyways i have changed it now – golldy Apr 13 '15 at 21:36
  • My examole escapes the brackets doesnt it? – Ewan Apr 14 '15 at 07:26
0

Here's what i did and got it working.

    private readonly static string reservedCharacters = "!*'();:@&=+$,/?%#[]";`

    //Neither of Uri.EscapeUriString and Uri.EscapeDataString methods encodes 
    //the RFC 2396 unreserved characters If you need these encoded then you 
    //will have to manually encode them
    public static string UrlEncode(string value)
    {
        if (String.IsNullOrEmpty(value))
            return String.Empty;

        var sb = new StringBuilder();

        foreach (char @char in value)
        {
            if (reservedCharacters.IndexOf(@char) == -1)
                sb.Append(@char);
            else
                sb.AppendFormat("%{0:X2}", (int)@char);
        }
        return sb.ToString();
    }
golldy
  • 1,279
  • 1
  • 15
  • 31