Index (zero based) must be greater than or... Working with the Bit.ly API

PsychoCoder picture PsychoCoder · Apr 11, 2011 · Viewed 13.9k times · Source

I'm working (actually more like playing) around with the Bit.ly API, and keep getting the error in the title of this question. So I'm going to show you the code and hopefuly someone can help me resolve this. First the client side code.

var x = service.GetClicks(url, service.BitlyLogin, service.BitlyAPIKey);
Console.WriteLine(x);

Console.ReadLine();

And this is the code that's being called

public List<int> GetClicks(string url, string login, string key)
{
    List<int> clicks = new List<int>();
    url = Uri.EscapeUriString(url);
    string reqUri =
        String.Format("http://api.bit.ly/v3/clicks?" +
        "login={0}&apiKey={1}&shortUrl={2}&format=xml" +
        login, key, url);

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUri);
    req.Timeout = 10000; // 10 seconds

    Stream stm = req.GetResponse().GetResponseStream();


    XmlDocument doc = new XmlDocument();
    doc.Load(stm);

    // error checking for xml
    if (doc["response"]["status_code"].InnerText != "200")
        throw new WebException(doc["response"]["status_txt"].InnerText);

    XmlElement el = doc["response"]["data"]["clicks"];
    clicks.Add(int.Parse(el["global_clicks"].InnerText));
    clicks.Add(int.Parse(el["user_clicks"].InnerText));

    return clicks;
}

As you can see it's very simple code, nothing complicated, and I can see nothing that causes this error. Anyone out there who has worked with(the full error is Index (zero based) must be greater than or equal to zero and less than the size of the argument list.) the Bit.ly API and can lend a hand?

Answer

F&#225;bioDRS picture FábioDRS · Apr 29, 2011

Instead this

string reqUri =
        String.Format("http://api.bit.ly/v3/clicks?" +
        "login={0}&apiKey={1}&shortUrl={2}&format=xml" + login, key, url);

Use this

string reqUri = String.Format("http://api.bit.ly/v3/clicks?login={0}&apiKey={1}&shortUrl={2}&format=xml", login, key, url);

Notice that I just changed the plus sign with the comma before "login, key, url);" at the end of the String.Format().