2009-12-16 84 views
3

我已經開始使用rapidshare.com API。我只是想知道從API和API調用中讀取答覆的最佳方式是什麼。閱讀rapidshare API響應的最佳方法是什麼?

說實話,我認爲API是遍佈各地的。有些答覆是用逗號分隔的,這很好。我遇到帳戶信息響應問題。這不是逗號分隔的,並且字段可能並不總是以相同的順序排列。

下面是一個示例響應: ACCOUNTID = 123456類型= PREM servertime = 1260968445添加時間= 1230841165 validuntil = 1262377165的用戶名= DOWNLOADER directstart = 1 protectfiles = 0 rsantihack = 0 plustrafficmode = 0鏡= jsconfig = 1個電子郵件=採取@ 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 |鏡像| jsconfig |電子郵件|許多| fpoints | ppoints | curfiles |如果數據的順序被認爲是隨機的,那麼我該如何確定哪個值是哪個值?如果數據的順序被認爲是隨機的,那麼如何確定哪個值是哪個值?

我只是好奇其他人是如何與此合作的。

感謝,

康納爾

回答

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

(它可能包含錯誤..但這個想法是明擺着的嗎?)

+0

我是新來的字典的想法,但它看起來很酷。我現在就放棄它。 謝謝! – conor 2009-12-16 18:10:48

0

你可能想分裂這個成某種形式的Dictionary對象,這樣就可以通過一鍵訪問值。

這裏有一個C#控制檯應用程序,使用.NET 3.5的工作的一個例子:

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

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

感謝您的幫助球員。

相關問題