C# API Wrapper TO vb.net API Wrapper

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von FAtheone.

    UTFSE
    google.com/search?q=c%23+to+vb
    ERSTES Ergebnis.

    Bin ich nicht nett?

    Hier

    VB.NET-Quellcode

    1. ' Usings
    2. Imports System.Collections.Generic
    3. Imports System.Text
    4. Imports System.Net
    5. Imports System.IO
    6. Imports System.Collections.Specialized
    7. Imports System.Windows.Forms
    8. Imports System.Collections
    9. Imports System.ComponentModel
    10. Friend Class VirusTotal
    11. Private apiKey As String = "SchnippSchnapp"
    12. Friend Function checkWebsite(websiteUrl As String) As String
    13. Try
    14. ' Erzeuge WebClient
    15. ServicePointManager.Expect100Continue = False
    16. Dim wClient As New WebClient()
    17. ' Erzeuge Post-Data Collection und füge den API-Key hinzu
    18. Dim postCollection As New NameValueCollection()
    19. postCollection.Add("key", apiKey)
    20. postCollection.Add("url", websiteUrl)
    21. wClient.QueryString = postCollection
    22. ' Sende Anfrage an VirusTotal
    23. Dim respondArray As Byte() = wClient.UploadValues("https://www.virustotal.com/api/scan_url.json", "POST", postCollection)
    24. ' Konvertiere Anfrage und werte diesen aus
    25. Dim respondString As String = System.Text.Encoding.UTF8.GetString(respondArray, 0, respondArray.Length)
    26. Dim values As Hashtable = jsonParser(respondString)
    27. ' Überprüfe ob Verarbeitet wurde
    28. If Convert.ToInt16(values("result")) = 1 Then
    29. ' Gebe HashID als Url zurück
    30. Return "https://www.virustotal.com/url-scan/report.html?id=" & Convert.ToString(values("scan_id"))
    31. Else
    32. Return "ERROR"
    33. End If
    34. Catch ex As Exception
    35. MessageBox.Show(ex.ToString())
    36. Return "ERROR"
    37. End Try
    38. End Function
    39. Friend Function checkFile(fileUrl As String) As String
    40. Try
    41. ' Überprüfe ob Datie vorhanden
    42. If Not File.Exists(fileUrl) Then
    43. Throw New FileNotFoundException("Datei nicht gefunden.")
    44. End If
    45. ' Erzeuge WebClient als Binary Stream
    46. ServicePointManager.Expect100Continue = False
    47. Dim wClient As New WebClient()
    48. wClient.Headers.Add("Content-Type", "binary/octet-stream")
    49. ' Erzeuge Post-Data Collection und füge den API-Key hinzu
    50. Dim postCollection As New NameValueCollection()
    51. postCollection.Add("key", apiKey)
    52. wClient.QueryString = postCollection
    53. ' Sende Anfrage an VirusTotal
    54. Dim respondArray As Byte() = wClient.UploadFile("https://www.virustotal.com/api/scan_file.json", "POST", fileUrl)
    55. ' Konvertiere Anfrage und werte diesen aus
    56. Dim respondString As String = System.Text.Encoding.UTF8.GetString(respondArray, 0, respondArray.Length)
    57. Dim values As Hashtable = jsonParser(respondString)
    58. ' Überprüfe ob Verarbeitet wurde
    59. If Convert.ToInt16(values("result")) = 1 Then
    60. ' Gebe HashID als Url zurück
    61. Return "https://www.virustotal.com/file-scan/report.html?id=" & Convert.ToString(values("scan_id"))
    62. Else
    63. Return "ERROR"
    64. End If
    65. Catch ex As Exception
    66. MessageBox.Show(ex.ToString())
    67. Return "ERROR"
    68. End Try
    69. End Function
    70. Friend Function jsonParser(jsonCode As String) As Hashtable
    71. Dim valuePairs As New Hashtable()
    72. ' Splitte die einzelnen Fragmente in einzelne Datenpakete
    73. Dim dataParts As String() = jsonCode.Split(","C)
    74. ' Für jedes Datenfragment
    75. For Each dataPart As String In dataParts
    76. ' Variable
    77. Dim VstartIndex As Integer = dataPart.IndexOf(""""C, 0) + 1
    78. Dim VendIndex As Integer = dataPart.IndexOf(""""C, VstartIndex)
    79. Dim variable As String = dataPart.Substring(VstartIndex, VendIndex - VstartIndex)
    80. ' Value
    81. Dim AstartIndex As Integer = dataPart.IndexOf(""""C, dataPart.IndexOf(":"C, VendIndex)) + 1
    82. Dim AendIndex As Integer = dataPart.IndexOf(""""C, AstartIndex + 1)
    83. Dim value As String = dataPart.Substring(AstartIndex, AendIndex - AstartIndex)
    84. ' Überprüfe Value
    85. If [String].IsNullOrEmpty(value.Replace(" ", "")) Then
    86. ' Es handelt sich um eine Zahl (keine "-Zeichen vorhanden)
    87. AstartIndex = dataPart.IndexOf(":"C, VendIndex + 1)
    88. ' Prüfe ob letztes Datenpaket
    89. If dataPart.IndexOf("}"C, AstartIndex) = -1 Then
    90. ' Nicht das letzte Datensegment
    91. value = dataPart.Substring(AstartIndex + 1, dataPart.Length - AstartIndex - 1).Replace(" ", "")
    92. Else
    93. ' Das letzte Datensegment
    94. Dim nextBracket As Integer = dataPart.IndexOf("}"C, AstartIndex) - 1
    95. value = dataPart.Substring(AstartIndex + 1, nextBracket - AstartIndex).Replace(" ", "")
    96. End If
    97. End If
    98. valuePairs.Add(variable, value)
    99. Next
    100. Return valuePairs
    101. End Function
    102. End Class