Use Case Scenario (C#)
You need to replace Resources.WebscantestSqlinj with your config file.
csharp
1using System;2using System.Diagnostics;3using System.IO;4using System.Net;5using System.Text;6using System.Threading;7using Newtonsoft.Json.Linq;8namespace NTOERestApiTest.Console9{10public class Program11{12private const string RootPath = "http://localhost:54073/AppSpiderEnterpriserest/v1/"; private const string Username = "wstclient";13private const string Password = "wstclient";14public static void Main(string[] args)15{16// login17var authResult = PostRequest("authentication/login", string.Format("{{ name: \"{0}\", password: \"{1}\" }}", Username, Password));18if (authResult.IsSuccess == null || !authResult.IsSuccess.Value)19{20System.Console.WriteLine(authResult.ErrorMessage.Value);21return;22}23string token = authResult.Token.Value;24System.Console.WriteLine("Logged in successfully.");25// get configs and check config with name WebscantestSqlInj doesn't exist(unable to create 2 configs with the same name)26var configsResult = GetRequest("config/getconfigs", token);27if (configsResult.IsSuccess == null || !configsResult.IsSuccess.Value)28{29System.Console.WriteLine(configsResult.ErrorMessage.Value);30return;31}32Guid? configId = null;33bool isConfigWithWebscantestSqlInjNameExist = false;34foreach (dynamic config in configsResult.Configs)35{36if (config.Name == "WebscantestSqlInj")37{38isConfigWithWebscantestSqlInjNameExist = true;39configId = config.Id;40System.Console.WriteLine("Config with name WebscantestSqlInj found.");41break;42} }43// Create new config if config with the same name doesn't exist44if (!isConfigWithWebscantestSqlInjNameExist)45{46string errorMessage;47configId = CreateConfig(token, out errorMessage);48if (configId == null)49{50System.Console.WriteLine(errorMessage);51return;52}53System.Console.WriteLine("Config with name WebscantestSqlInj created.");54}55// run scan56var runScanResult = PostRequest("scan/runscan", string.Format("{{\"configId\":57\"{0}\" }}", configId), token);58if (runScanResult.IsSuccess == null || !runScanResult.IsSuccess.Value)59{60System.Console.WriteLine(runScanResult.ErrorMessage.Value);61return;62}63var scanId = runScanResult.Scan.Id;64System.Console.WriteLine("Scan started. Scan ID: {0}", scanId);65// waiting for scan finished66var stopwatch = new Stopwatch(); stopwatch.Start();67bool isScanFinished = false; while (!isScanFinished)68{69System.Console.Write("{0} minutes and {1} seconds elapsed.",70stopwatch.Elapsed.Minutes, stopwatch.Elapsed.Seconds);71System.Console.Write("\r");72Thread.Sleep(5000);73var isScanFinishedResult =74GetRequest(string.Format("scan/isscanfinished?scanId={0}", scanId), token);75if (isScanFinishedResult.IsSuccess == null ||76!isScanFinishedResult.IsSuccess.Value)77{78System.Console.WriteLine(isScanFinishedResult.ErrorMessage.Value);79return;80}81isScanFinished = isScanFinishedResult.Result;82}83stopwatch.Stop();84System.Console.WriteLine();85System.Console.WriteLine("Scan finished.");86// check finished scan has report87var scanHasReportResult = GetRequest(string.Format("scan/hasreport?scanId={0}",88scanId), token);89if (scanHasReportResult.IsSuccess == null || !scanHasReportResult.IsSuccess.Value)90{91System.Console.WriteLine(scanHasReportResult.ErrorMessage.Value);92return;93}94if (!scanHasReportResult.Result.Value)95{96System.Console.WriteLine("Scan finished and has no report.");97return;98}99SaveFile(string.Format("report/getreportzip?scanid={0}", scanId),100"ReportAllFiles.zip", token);101System.Console.WriteLine("Report saved with name ReportAllFiles.zip.");102}103private static Guid? CreateConfig(string token, out string errorMessage)104{105token);106errorMessage = null;107var engineGroupsResult = GetRequest("enginegroup/getenginegroupsforclient",108if (engineGroupsResult.EngineGroups == null)109{110errorMessage = engineGroupsResult.ErrorMessage.Value;111return null;112}113if (engineGroupsResult.EngineGroups.Count == 0)114{115errorMessage = "Where are no engine groups assigned to current client.";116return null;117}118var engineGroupId = engineGroupsResult.EngineGroups[0].Id;119var configJson = string.Format(@"{{120'DefendEnabled': true,121'MonitoringDelay': 0,122'MonitoringTriggerScan': true,123'Id': null,124'Name': 'WebscantestSqlInj',125'ClientId': null,126'EngineGroupId': '{0}',127'Monitoring': true,128'IsApproveRequired': false,129'Xml': '{1}'130}}", engineGroupId, Resources.WebscantestSqlInj /* TODO: replace with your config131string (scfg file) */); // ', ", \ symbols must be escaped in config string (\', \", \\)132var createConfigResult = PostMultipartRequest("config/saveconfig", "config",133configJson, token);134if (createConfigResult.IsSuccess == null || !createConfigResult.IsSuccess.Value)135{136errorMessage = createConfigResult.ErrorMessage.Value;137return null;138}139return createConfigResult.Config.Id;140}141private static void SaveFile(string url, string fileName, string token)142{143using (var webClient = new WebClient())144{145webClient.Headers["Authorization"] = "Basic " + token;146webClient.DownloadFile(RootPath + url, fileName);147}148}149private static dynamic GetRequest(string url, string token)150{151string responseStr;152try153{154var httpRequest = (HttpWebRequest)WebRequest.Create(RootPath + url);155httpRequest.Accept = "application/json";156httpRequest.ContentType = "application/json";157httpRequest.Method = "GET";158httpRequest.Headers["Authorization"] = "Basic " + token;159var response = httpRequest.GetResponse();160using (Stream data = response.GetResponseStream()) using (var reader = new StreamReader(data))161{162responseStr = reader.ReadToEnd();163}164}165catch (WebException e)166{167using (WebResponse response = e.Response)168{169} }170using (Stream data = response.GetResponseStream())171using (var reader = new StreamReader(data))172{173responseStr = reader.ReadToEnd();174}175return JObject.Parse(responseStr);176}177private static dynamic PostRequest(string url, string jsonData, string token = "") {178string responseStr;179try180{181var httpRequest = (HttpWebRequest)WebRequest.Create(RootPath + url);182httpRequest.Accept = "application/json";183httpRequest.ContentType = "application/json";184httpRequest.Method = "POST";185httpRequest.Headers["Authorization"] = "Basic " + token;186string jsonContent = jsonData;187var encoding = new ASCIIEncoding();188byte[] bytes = encoding.GetBytes(jsonContent);189Stream newStream = httpRequest.GetRequestStream();190newStream.Write(bytes, 0, bytes.Length);191newStream.Close();192var response = httpRequest.GetResponse();193using (Stream data = response.GetResponseStream()) using (var reader = new StreamReader(data))194{195responseStr = reader.ReadToEnd();196}197}198catch (WebException e)199{200using (WebResponse response = e.Response)201{202using (Stream data = response.GetResponseStream())203} }204using (var reader = new StreamReader(data))205{206responseStr = reader.ReadToEnd();207}208return JObject.Parse(responseStr);209}210private static dynamic PostMultipartRequest(string url, string fileName, string211fileData, string token = "")212{213string responseStr;214try215{216newLine +217var boundary = "------------------------" + DateTime.Now.Ticks;218var newLine = Environment.NewLine;219var propFormat = "--" + boundary + newLine +220"Content-Disposition: form-data; name=\"{0}\"" + newLine +221"{1}" + newLine;222var httpRequest = (HttpWebRequest)WebRequest.Create(RootPath + url);223httpRequest.Accept = "application/json";224httpRequest.ContentType = "multipart/form-data; boundary=" + boundary;225httpRequest.Method = "POST";226httpRequest.Headers["Authorization"] = "Basic " + token;227using (var reqStream = httpRequest.GetRequestStream())228{229var reqWriter = new StreamWriter(reqStream);230var tmp = string.Format(propFormat, fileName, fileData);231reqWriter.Write(tmp);232reqWriter.Write("--" + boundary + "--");233reqWriter.Write("\r\n");234reqWriter.Flush();235}236var response = httpRequest.GetResponse();237using (Stream data = response.GetResponseStream()) using (var reader = new StreamReader(data))238{239responseStr = reader.ReadToEnd();240}241}242catch (WebException e)243{244using (WebResponse response = e.Response)245{246using (Stream data = response.GetResponseStream())247using (var reader = new StreamReader(data))248{249} }250} }251responseStr = reader.ReadToEnd();252}253return JObject.Parse(responseStr);
Did this page help you?