3

我已经开始使用 rapidshare.com API。我只是想知道从 API 调用中读取回复的最佳方式是什么。

老实说,我认为 API 无处不在。有些回复是逗号分隔的,这很好。我遇到了帐户信息响应问题。这不是逗号分隔的,并且字段可能并不总是以相同的顺序排列。

这是一个示例响应: accountid=123456 type=prem servertime=1260968445 addtime=1230841165 validuntil=1262377165 username=DOWNLOADER directstart=1 protectfiles=0 rsantihack=0 plustrafficmode=0 mirrors= jsconfig=1 email=take@hike.com lot= 0 fpoints=12071 ppoints=10 curfiles=150 curspace=800426795 bodkb=5000000 premkbleft=23394289 ppointrate=93

我认为正则表达式是去这里的方式。这是我的表达式,它似乎涵盖了所有包含值的响应: (accountid|type|servertime|addtime|validuntil|username|directstart|protectfiles|rsantihack|plustrafficmode|mirrors|jsconfig|email|lots|fpoints|ppoints|curfiles|curspace |bodkb|premkbleft|ppointrate|refstring|cookie)\=[\w._@]+

如果数据的顺序被认为是随机的,那么我如何确定哪个值是哪个?

我只是好奇其他人是如何处理这个问题的。

谢谢,

康纳

4

3 回答 3

2

我假设 c#。

string[] s = @"accountid=123456 type=prem servertime=1260968445 addtime=1230841165 validuntil=1262377165 username=DOWNLOADER directstart=1 protectfiles=0 rsantihack=0 plustrafficmode=0 mirrors= jsconfig=1 email=take@hike.com lots=0 fpoints=12071 ppoints=10 curfiles=150 curspace=800426795 bodkb=5000000 premkbleft=23394289 ppointrate=93".Split(" ");
var params = new Dictionary<string, string>();
foreach(var l in s)
{
 var tmp = l.Split("=");
 params[tmp[0]] = params[tmp[1]];
}

(它可能包含错误..但这个想法很明显?)

于 2009-12-16T13:33:57.190 回答
0

您可能希望将其拆分为某种 Dictionary 对象,以便您可以通过键访问该值。

下面是一个适用于 .NET 3.5 的 C# 控制台应用程序示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace SO
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = @"accountid=123456 type=prem servertime=1260968445";

            string pattern = @"(?<Key>[^ =]+)(?:\=)(?<Value>[^ ]+)(?=\ ?)";

            Dictionary<string, string> fields = 
                (from Match m in Regex.Matches(input, pattern)
                   select new
                   {
                       key = m.Groups["Key"].Value,
                       value = m.Groups["Value"].Value
                   }
                ).ToDictionary(p => p.key, p => p.value);

            //iterate over all fields
            foreach (KeyValuePair<string, string> field in fields)
            {
                Console.WriteLine(
                    string.Format("{0} : {1}", field.Key, field.Value)
                );
            }

            //get value from a key
            Console.WriteLine(
                string.Format("{0} : {1}", "type", fields["type"])
            );

        }
    }
}

链接到 PHP 中的另一个示例:

如何使用 rapidshare API 来获取帐户详细信息 ?? PHP 问题

于 2009-12-16T14:00:13.553 回答
0

这是我所做的。

它基本上只是 Yossarian 的代码的工作版本。

            // Command to send to API
        String command = "sub=getaccountdetails_v1&type=prem&login="+Globals.username+"&password="+Globals.password;

        // This will return the response from rapidshare API request.
        // It just performs @ webrequest and returs the raw text/html. It's only a few lines. Sorry I haven't included it here.
        String input  = executeRequest(command);

        input = input.Trim();
        string[] s = input.Split('\n');

        Dictionary<string,string> terms = new Dictionary<string, string>();
        foreach(var l in s)
        {
             String[] tmp = l.Split('=');
             terms.Add(tmp[0], tmp[1]);
        }
        foreach (KeyValuePair<String, String> term in terms)
        {
            txtOutput.Text += term.Key + " :: " + term.Value+"\n";
        }

谢谢你们的帮助。

于 2009-12-17T21:45:20.890 回答