converting a Classic ASP xmlhttp.open function to Razor cshtml RRS feed

  • Question

  • User-1466445078 posted


    Please can someone help me. On my site I have an area that displays a bit.ly shortened URL. The URL that is shortened is dynamically created. I have a bit.ly API Key/Username and for the past few years I've been using this code on my Classic ASP sites to create the shortened URL dynamically.

    Function bitlyThis(dynamiclongurl)
    apikey = "myapikey"
    url = "http://api.bitly.com/v3/shorten?login=myusername&apiKey=" & apikey & "&longUrl=" & dynamiclongurl & "&format=txt" 
    set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
    xmlhttp.open "GET", url, false 
    xmlhttp.send "" 
    bitlyThis = xmlhttp.responseText 
    set xmlhttp = nothing 

    I want to use the same process on my ASP.NET Web Pages (cshtml) site. I've looked at the bit.ly API documentation and tried various bit.ly Nuget packages but none of them worked for me (I get various errors about services missing, etc). I tried URLShortener from Nuget but that also brings up an error.

    I only want to use the "shortener" bit of the bit.ly API - is there anyway to recreate my old Classic ASP function in C# for Web Pages - I've spent all day trying to get this to work and I'm really stuck.

    Any help will be greatly appreciated.


    Wednesday, August 13, 2014 9:19 AM


All replies

  • User-821857111 posted

    If you want to make an HTTP request from server code, you can use the WebClient.DownloadString  method: http://msdn.microsoft.com/en-us/library/fhd1f0sw(v=vs.110).aspx

    @using System.Collections.Specialized
        var url = "http://api.bitly.com/v3/shorten";
        string response;
        var qs = new NameValueCollection();
        qs.Add("apiKey", apiKey);
        qs.Add("longUrl", dynamiclongurl);
        qs.Add("format", "txt");
        using (var client = new WebClient()){
            client.QueryString = qs;
            response = client.DownloadString(url);

    If you want to make this reusable, have a look at the options here: http://www.mikesdotnetting.com/Article/227/Migrating-Classic-ASP-To-ASP.NET-Razor-Web-Pages-Part-Three--Include-Files-And-Reusable-Code

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 13, 2014 9:59 AM
  • User-1466445078 posted

    Hi Mike,

    Thanks for the response. I gave it a go but it seems you can only use that method with POST (as the method) - GET doesn't work.

    By the way, I bought your book - it's very well written and very easy to follow.

    Thanks again.

    Wednesday, August 13, 2014 11:14 AM
  • User-821857111 posted

    I changed my reply when I realised that the first suggestion only works with POST. The current suggestion works with GET. It's the default for DownloadString. Have another go.

    Pleased you like the book! 

    Wednesday, August 13, 2014 11:26 AM
  • User-1466445078 posted

    You're a genius - that worked great!

    Thank you so much.

    Wednesday, August 13, 2014 11:38 AM