同步病毒总代码到 Rx 启用异步(也许还有一些 JSON 解析)

本文关键字:JSON 解析 也许 代码 病毒 Rx 异步 启用 同步 | 更新日期: 2023-09-27 18:36:44

上次我在这里发布问题时,每个人都提供了一些很好的指导来解决我的问题。在时间上前进,这是另一个。我正在尝试重做我拥有的一个小助手工具,该工具可以检查 URL 和文件针对 VirusTotal 以获取一些基本信息。下面的代码运行良好,但锁定了 UI。有人告诉我,我应该研究Rx,并喜欢阅读它,但似乎无法让我的头缠绕在它周围。所以现在问题来了,设计以下代码以使其利用 Rx 的最佳方法是什么,以便它是异步的,并且在它执行它的事情时让我的 UI 单独存在。VirusTotal还利用多级JSON进行响应,因此,如果有人有将其集成到其中的好方法,那就更好了。

class Virustotal
{
    private string APIKey = "REMOVED";
    private string FileReportURL = "https://www.virustotal.com/vtapi/v2/file/report";
    private string URLReportURL = "http://www.virustotal.com/vtapi/v2/url/report";
    private string URLSubmitURL = "https://www.virustotal.com/vtapi/v2/url/scan";
    WebRequest theRequest;
    HttpWebResponse theResponse;
    ArrayList  theQueryData;
    public string GetFileReport(string checksum) // Gets latest report of file from VT using a hash (MD5 / SHA1 / SHA256)
    {
        this.WebPostRequest(this.FileReportURL);
        this.Add("resource", checksum);
        return this.GetResponse();
    }
    public string GetURLReport(string url) // Gets latest report of URL from VT
    {
        this.WebPostRequest(this.URLReportURL);
        this.Add("resource", url);
        this.Add("scan", "1"); //Automatically submits to VT if no result found
        return this.GetResponse();
    }
    public string SubmitURL(string url) // Submits URL to VT for insertion to scanning queue
    {
        this.WebPostRequest(this.URLSubmitURL);
        this.Add("url", url);
        return this.GetResponse();
    }
    public string SubmitFile() // Submits File to VT for insertion to scanning queue
    {
        // File Upload code needed
        return this.GetResponse();
    }
    private void WebPostRequest(string url)
    {
        theRequest = WebRequest.Create(url);
        theRequest.Method = "POST";
        theQueryData = new ArrayList();
        this.Add("apikey", APIKey);
    }
    private void Add(string key, string value)
    {
        theQueryData.Add(String.Format("{0}={1}", key, Uri.EscapeDataString(value)));
    }
    private string GetResponse()
    {
        // Set the encoding type
        theRequest.ContentType="application/x-www-form-urlencoded";
        // Build a string containing all the parameters
        string Parameters = String.Join("&",(String[]) theQueryData.ToArray(typeof(string)));
        theRequest.ContentLength = Parameters.Length;
        // We write the parameters into the request
        StreamWriter sw = new StreamWriter(theRequest.GetRequestStream());
        sw.Write(Parameters);
        sw.Close();
        // Execute the query
        theResponse =  (HttpWebResponse)theRequest.GetResponse();
        StreamReader sr = new StreamReader(theResponse.GetResponseStream());
        return sr.ReadToEnd();
    }
}

同步病毒总代码到 Rx 启用异步(也许还有一些 JSON 解析)

你的代码写得很差,这使得它更难异步 - 主要是三个类级变量。在 Rx 中编码时,您需要考虑"函数式编程"而不是"OOP"——因此没有类级变量。

所以,我所做的是这样的 - 我重新编码了 GetResponse 方法以将所有状态封装到单个调用中 - 我让它返回IObservable<string>而不仅仅是string

公共函数现在可以这样编写:

public IObservable<string> GetFileReport(string checksum)
{
    return this.GetResponse(this.FileReportURL,
        new Dictionary<string, string>() { { "resource", checksum }, });
}
public IObservable<string> GetURLReport(string url)
{
    return this.GetResponse(this.URLReportURL,
        new Dictionary<string, string>()
            { { "resource", url }, { "scan", "1" }, });
}
public IObservable<string> SubmitURL(string url)
{
    return this.GetResponse(this.URLSubmitURL,
        new Dictionary<string, string>() { { "url", url }, });
}
public IObservable<string> SubmitFile()
{
    return this.GetResponse("UNKNOWNURL", new Dictionary<string, string>());
}

GetResponse看起来像这样:

private IObservable<string> GetResponse(
    string url,
    Dictionary<string, string> theQueryData)
{
    return Observable.Start(() =>
    {
        var theRequest = WebRequest.Create(url);
        theRequest.Method = "POST";
        theRequest.ContentType="application/x-www-form-urlencoded";
        theQueryData.Add("apikey", APIKey);
        string Parameters = String.Join("&",
            theQueryData.Select(x =>
                String.Format("{0}={1}", x.Key, x.Value)));
        theRequest.ContentLength = Parameters.Length;
        using (var sw = new StreamWriter(theRequest.GetRequestStream()))
        {
            sw.Write(Parameters);
            sw.Close();
        }
        using (var theResponse =  (HttpWebResponse)theRequest.GetResponse())
        {
            using (var sr = new StreamReader(theResponse.GetResponseStream()))
            {
                return sr.ReadToEnd();
            }
        }
    });
}

我还没有实际测试过这个 - 我没有初学者的 APIKEY - 但它应该可以正常工作。让我知道你怎么做。