[英]Best way to read rapidshare API response?
我已经开始使用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 | type | servertime | addtime | validuntil | username | directstart | protectfiles | rsantihack | plustrafficmode | mirrors | jsconfig | email | lots | fpoints | ppoints | curfiles | curspace | bodkb | premkbleft | ppointrate | refstring |饼干)\\ = [\\ W ._ @] +
如果数据的顺序被认为是随机的,那么我该如何确定哪个值是哪个?
我只是好奇其他人是如何使用它的。
谢谢,
康纳尔
我假设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]];
}
(它可能包含错误..但这个想法很明显?)
您可能希望将其拆分为某种类型的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中的另一个示例:
这就是我所做的。
它基本上只是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";
}
谢谢你的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.