locked
URLEncode in C# in CLR Stored Procedure RRS feed

  • Question

  • Hi,
     
    I've got a problem where i require to make a request to an external web site via a CLR SP. Making the request is fine but i do not have the functionality to perform a URLEncode and Decode.

    When i add a reference my only option is System.Web.Services which doesnt have these functions.

    Anyone any ideas?

    I created a crude method to urlencode the string

    string allowedChars = "[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz023456789()''*-._! ]";
    string urlencodedXML = "";
    foreach (char c in sXml)
    {
        if(allowedChars.Contains(c.ToString()))
        {
            urlencodedXML = urlencodedXML + c.ToString();
        }
        else
        {
            int tmp = Char.Parse(c.ToString());
            urlencodedXML = urlencodedXML +
    "%" + tmp.ToString();
        }
    }

    I haven't checked this yet. If i am forced to go down this route i am unsure how i would do the decode. My issue is if there is a number in the request (e.g. 22) - how would i distinguish between %22 (whitespace) and the number? Anyone with any sample code?

    Thanks
    Dom

    Thursday, September 18, 2008 8:07 AM

Answers

  • Hi,

    While I have honestly never wrote any CLR stored procedure, as it is a .Net assembly you can probably reference any other assemblies. So the easiest way would be to reference the System.Web library in your project and then use the following static method to url-encode your string:

    HttpUtility.UrlEncode(String)

    Example:

    using System.Web

    ...

    string urlEncodedString = HttpUtility.UrlEncode(inputString);


    HTH,

    Francois Malgreve
    http://www.malgreve.net
    • Marked as answer by jack 321 Monday, September 22, 2008 3:52 AM
    Thursday, September 18, 2008 9:52 AM
  • Hi Dominnic,

    Actually, “System.Web” assembly is just a .Net assembly in GAC; there are no differences in using this assembly and other regular assembly, such as “System.Windows.Forms”, just add reference of the “System.web.dll” assembly and “using System.web” namespace, after to do is, we can also use it in a windows application.

    Regards,

    Xun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706
    • Edited by jack 321 Monday, September 22, 2008 3:52 AM add a sentence
    • Marked as answer by jack 321 Wednesday, September 24, 2008 9:16 AM
    Monday, September 22, 2008 3:50 AM

All replies

  • Hi,

    While I have honestly never wrote any CLR stored procedure, as it is a .Net assembly you can probably reference any other assemblies. So the easiest way would be to reference the System.Web library in your project and then use the following static method to url-encode your string:

    HttpUtility.UrlEncode(String)

    Example:

    using System.Web

    ...

    string urlEncodedString = HttpUtility.UrlEncode(inputString);


    HTH,

    Francois Malgreve
    http://www.malgreve.net
    • Marked as answer by jack 321 Monday, September 22, 2008 3:52 AM
    Thursday, September 18, 2008 9:52 AM
  • Thanks for the reply. This is an excellent idea and one that i thought woudl work.

    However the System.Web assembly isnt available when i try to add a reference, which means that i can not use HttpUtility.

    thanks
    Thursday, September 18, 2008 12:31 PM
  • Hi Dominnic,

    Actually, “System.Web” assembly is just a .Net assembly in GAC; there are no differences in using this assembly and other regular assembly, such as “System.Windows.Forms”, just add reference of the “System.web.dll” assembly and “using System.web” namespace, after to do is, we can also use it in a windows application.

    Regards,

    Xun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706
    • Edited by jack 321 Monday, September 22, 2008 3:52 AM add a sentence
    • Marked as answer by jack 321 Wednesday, September 24, 2008 9:16 AM
    Monday, September 22, 2008 3:50 AM
  • Xun,
    Thanks for the reply.

    What i had to do was to add the assmbley to SQL server using:

    CREATE ASSEMBLY [System.Web]
    AUTHORIZATION dbo
    FROM 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Web.dll'
    WITH PERMISSION_SET = UNSAFE;

    After that i could use the HTTPUtility.

    Monday, September 22, 2008 10:06 AM