2012-09-12 132 views
2

所以我得到這個VirusTotal Api的用法?

public class VirusTotal 
{ 
    public string APIKey; 
    string scan = "https://www.virustotal.com/api/scan_file.json"; 
    string results = "https://www.virustotal.com/api/get_file_report.json"; 

    public VirusTotal(string apiKey) 
    { 
     ServicePointManager.Expect100Continue = false; 
     APIKey = apiKey; 
    } 

    public string Scan(string file) 
    { 
     var v = new NameValueCollection(); 
     v.Add("key", APIKey); 
     var c = new WebClient() { QueryString = v }; 
     c.Headers.Add("Content-type", "binary/octet-stream"); 
     byte[] b = c.UploadFile(scan, "POST", file); 
     var r = ParseJSON(Encoding.Default.GetString(b)); 
     if (r.ContainsKey("scan_id")) 
     { 
      return r["scan_id"]; 
     } 
     throw new Exception(r["result"]); 
    } 

    public string GetResults(string id) 
    { 
     Clipboard.SetText(id); 
     var data = string.Format("resource={0}&key={1}", id, APIKey); 
     var c = new WebClient(); 
     string s = c.UploadString(results, "POST", data); 
     var r = ParseJSON(s); 
     foreach (string str in r.Values) 
     { 
      MessageBox.Show(str); 
     } 
     if (r["result"] != "1") 
     { 
      throw new Exception(r["result"]); 
     } 
     return s; 
    } 

    private Dictionary<string, string> ParseJSON(string json) 
    { 
     var d = new Dictionary<string, string>(); 
     json = json.Replace("\"", null).Replace("[", null).Replace("]", null); 
     var r = json.Substring(1, json.Length - 2).Split(','); 
     foreach (string s in r) 
     { 
      d.Add(s.Split(':')[0], s.Split(':')[1]); 
     } 
     return d; 
    } 
} 

但被做我輸入的URL我會awsume它的掃描,但我怎麼retrive掃描回來,這一點,開始掃描proccess

抱歉,但即時通訊新在apis和我真的沒有得到webclient thx爲我的幫助

回答

2

你實際上不需要在這裏指定任何URL。

當您上傳要掃描的文件時,您會收到由VirusTotal生成的唯一請求ID。該ID是一個返回值Scan函數。如果你將這個值存儲在一個變量中,然後在調用GetResults時指定它,那麼你應該得到你的結果。

代碼如下:

VirusTotal vtObject = new VirusTotal("%Your_API_key_here%"); 
string resultID = vtObject.Scan("%your_file_name_here%"); 
string results = vtObject.GetResults(resultID); 

另請注意,文件掃描需要一些時間,所以你將最有可能在results收到類似「您的文件排隊掃描,稍後回來」。您可能需要在一定的時間間隔後再調用GetResults,以便在VT處理文件後可以獲得實際的掃描數據。

+0

Im仍然困惑我puted我的網址在你的文件名在這裏+我的api密鑰在這裏你的api鍵 – PenguinSmex

+0

你想從url掃描一個文件嗎?然後,您需要:1)將文件下載到本地硬盤驅動器,並在指定掃描路徑時指定路徑或2)使用VT API的另一個功能,特別設計用於掃描URL(請參閱[文檔](https:/ /www.virustotal.com/documentation/public-api/),部分「發送和掃描網址」) –

+0

我想掃描一個網址,我不想使用網頁瀏覽器,我希望它能夠檢測到它是否被檢測到 – PenguinSmex