1

我正在使用 Bit.ly API(实际上更像是在玩),并不断收到此问题标题中的错误。因此,我将向您展示代码,并希望有人可以帮助我解决此问题。首先是客户端代码。

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

Console.ReadLine();

这是被调用的代码

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;
}

正如您所看到的,它是非常简单的代码,没有什么复杂的,而且我看不到任何导致此错误的东西。任何使用过Bit.ly API的人(完整的错误是索引(从零开始)必须大于或等于零并且小于参数列表的大小。)Bit.ly API并且可以伸出援手吗?

4

3 回答 3

4

取而代之的是

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

用这个

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

请注意,我只是在String.Format()末尾的“ login, key, url); ”之前用逗号更改了加号。

于 2011-04-29T10:54:43.243 回答
3

我将它缩小到我使用 string.Format 构建数组的地方,并且 string.Format 中的内容比预期的要少。我让它进入索引 3,但只填充到索引 2

于 2011-04-14T01:24:49.573 回答
1

不适用于您的具体情况,但我遇到了这个问题:确保,如果您有多个参数,则将它们作为对象数组而不是 IEnumerable 发送:

IEnumerable<object> myArgs = ...;
string toFormat = "{0} xyz {1}";

String.Format(toFormat, myArgs);
// ERROR, since myArgs is one argument whereas the string template requires two

String.Format(toFormat, myArgs.ToArray());
// Valid, as the Format() accepts an array of objects to fill all arguments in the string
于 2014-01-07T10:51:59.650 回答