索引(从零开始)必须大于或... 使用 Bit.ly API

发布于 2024-10-31 05:43:33 字数 1295 浏览 1 评论 0原文

我正在使用 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(完整错误是索引(从零开始)必须大于或等于零且小于参数列表的大小。)的人都可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

但可醉心 2024-11-07 05:43:34

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);”之前更改了加号和逗号。

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().

千里故人稀 2024-11-07 05:43:34

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

I narrowed it down to a place where I was using string.Format to build an array and has less in the string.Format than what was supposed to. I had it go to Index 3 but only filled to Index 2

溺渁∝ 2024-11-07 05:43:34

不适合您的具体情况,但我遇到了这个:如果您有多个参数,请确保将它们作为对象数组而不是 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

Not for your specific case, but I ran into this: make sure that, if you have multiple parameters, you send them as an array of objects instead of an 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文