我开始使用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 lots = 0 fpoints = 12071 ppoints = 10个curfiles = 150个curspace = 800426795 bodkb = 5000000 premkbleft = 23394289 ppointrate = 93
我认为正则表达式是走到这里的方式。这是我的表达似乎所有包含值的响应: (ACCOUNTID |类型| servertime |添加时间| validuntil |用户名| directstart | protectfiles | rsantihack | plustrafficmode |镜| jsconfig |电子邮件|大量| fpoints | ppoints | curfiles | curspace | bodkb | premkbleft | ppointrate | refstring |饼干)\ = [ \ W ._ @] +
如果数据的顺序被认为是随机的,那么我该如何确定哪个值是哪个?
我只是好奇其他人是如何使用它的。
谢谢,
康纳
答案 0 :(得分: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]];
}
(它可能包含错误..但这个想法很明显?)
答案 1 :(得分: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中的另一个示例:
How to use rapidshare API to get Account Details ?? PHP question
答案 2 :(得分: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";
}
感谢您的帮助。