我正在尝试将外部API的使用与我的C#WPF客户端应用程序集成在一起以执行POST和GET调用。 https://developers.virustotal.com/reference。
我有以下代码:
InfoProcessor.xaml.cs
public static async Task<InfoModel> LoadInfo(string infoVar)
{
string apiKey = "xxxxxxxxxxxxx";
//string resourceKey = "efbb7149e39e70c84fe72c6fe8cef5d379fe37e7238d19a8a4536fb2a3146aed";
string resourceKey = infoVar;
string url = $"https://www.virustotal.com/vtapi/v2/file/report?apikey={apiKey}&resource={resourceKey}";
//await Task.Delay(30000);
using (HttpResponseMessage response = await ApiStartup.ApiClient.Getasync(url))
{
if (response.IsSuccessStatusCode)
{
InfoModel info = await response.Content.ReadAsAsync<InfoModel>();
return info;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
public static async Task<InfoModel> PostFileInfo(string filePath)
{
string url = "https://www.virustotal.com/vtapi/v2/file/scan";
ApiStartup.ApiClient.DefaultRequestHeaders.Add("User-Agent","VT API Service");
using (var content = new MultipartFormDataContent())
{
var path = filePath;
string assetName = Path.GetFileName(path);
FileStream fs = File.OpenRead(path);
var streamContent = new StreamContent(fs);
streamContent.Headers.Add("Content-Type","application/octet-stream");
streamContent.Headers.Add("Content-Disposition","form-data; name=\"file\"; filename=\"" + Path.GetFileName(path) + "\"");
content.Add(streamContent,"file",Path.GetFileName(path));
content.Add(CreateApiPart());
//content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
Task<HttpResponseMessage> message = ApiStartup.ApiClient.PostAsync(url,content);
InfoModel info = await message.Result.Content.ReadAsAsync<InfoModel>();
return info;
}
}
ScanPage.xaml.cs
string filePath;
private void browseFileButton_Click(object sender,RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
openFileDialog.Filter = "All files (*.*)|*.*";
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (openFileDialog.ShowDialog() == true)
{
filePath = openFileDialog.FileName;
fileLocation.Text = filePath;
}
}
private async void uploadFileButton_Click(object sender,RoutedEventArgs e)
{
if (filePath != null)
{
//await InfoProcessor.PostFileInfo(filePath,"https://www.virustotal.com/vtapi/v2/url/scan",);
responseText.Text = "File queued for scanning,please wait a moment...";
//var resultInfo = await InfoProcessor.PostInfo(filePath);
var resultInfo = await InfoProcessor.PostFileInfo(filePath);
await Task.Delay(60000);
var resultInfo2 = await InfoProcessor.LoadInfo(resultInfo.Resource);
//await Task.Delay(30000);
responseText.Text = $"Scan Completed,MD5 checksum of file is {resultInfo2.Sha256} \n {resultInfo2.Positives} out of {resultInfo2.Total} {resultInfo2.Permalink} scan engines has detected to be potentially malicious";
}
else
{
MessageBox.Show("please upload a file");
}
}
选择并上传文件后,将使用PostFileInfo函数上传和扫描文件,然后将响应内容resource
id插入到LoadInfo
函数中作为参数。
在扫描文件时,应加载的其余响应内容(例如md5 / sha256 / sha1值)将为空,直到扫描完成为止,这意味着我将需要执行在PostFileInfo和LoadInfo函数之间的Task.Delay函数。
但是,即使在函数之间延迟60秒之后,结果也不会出现在responseText.Text中,直到我在一段时间后再次调用它为止。我可以正确实现Task.Delay函数吗?