Encode a URL in C# with a Utility Choreo


Temboo does a lot more than just work with APIs. We've got a growing collection of programming utilities that will make your life much easier. Here we'll show you how to use these handy code shortcuts by working through a URL encoding example.

You might ask "why is URL encoding important?". Well, URLs can only be sent over the Internet using the ASCII character-set. Since URLs often contain non-ASCII characters, they need to be converted so they only contain valid characters. The general rule is that unsafe characters are replaced with a % followed by two hexadecimal digits. Spaces are replaced by either a plus sign (+) or with %20. You've probably seen URLs full of strange looking characters, and now you know why!

The good news is that you don't actually have to worry about the specifics. Our URL encoding utility will do all the work for you.

Get Set Up

1 Log in to Temboo. If you don't already have an account, you can register for free.

2 Make sure you've downloaded the Temboo C# SDK, and added TembooSDK.dll as a reference to your "C# Console Application" project as described in our C# getting started tutorial.

Test the Choreo

3 Go to the Utilities > Encoding > URLEncode Choreo in our library. Select C# from the drop down menu at the top of the page.

4 Enter some text that you want to URL encode. It should contain illegal characters, like spaces, accents, and punctuation. Here's some text you can use:

I'm Enc0ding URL$ wITh T3mBo0

5 Click Generate Code to test the Choreo from our website, and you'll see the the URL encoded result returned by the Choreo.

Create Your C# Program

6 To call the URL encoding Choreo from a C# program, copy the generated code from your browser directly into a C# class and try running it there. Your code should look something like this:

using System;
using Temboo.Core;
using Temboo.Library.Utilities.Encoding;

namespace TestProject
{
    class URLEncodeSomeText
    {
        static void Main(string[] args)
        {
            // Instantiate a TembooSession object using your Account name and Application key
            TembooSession session = new TembooSession("ACCOUNT_NAME", "APP_NAME", "APP_KEY");

            // Instantiate the Choreo using the TembooSession
            URLEncode urlEncodeChoreo = new URLEncode(session);

            // Set inputs
            urlEncodeChoreo.setText("I'm Enc0ding URL$ wITh T3mBo0");
     
            // Execute Choreo
            URLEncodeResultSet urlEncodeResults = urlEncodeChoreo.execute();

            // Print results
            Console.WriteLine(urlEncodeResults.URLEncodedText);
        }
    }
}

7 At this point, the app is ready to run but by default it will exit the console session immediately. To make sure our output doesn't disappear, let's update the code to wait for input before terminating. Add the following code to the end of your Main method, immediately under the Console.WriteLine(...) statement:

            // Wait for input, to prevent the Terminal from exiting immediately
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();

8 Run the code and the URL encoded version of the text will be printed in the console.

What next?

We're all finished! Now you know about Temboo's range of handy developer utilities and how to access them via our C# SDK. Check out the rest of the 2000+ Choreos in our Library and see how you can combine them with our utilities to build something amazing.

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.

Need help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back