none
Convert Python "GET" Statement to VB.NET RRS feed

  • Question

  • Hello All,

    If someone could, would you be able to help me re-code a simple Python GET statement to VB.Net.  I am having all sorts of issues trying to figure out Webrequest.  I've never had to use it before.  I'm not even sure if that's the way to go for this issue.

    Thank you in advance,

    -Ron

    Here is the Python Statement..

    import requests 
    params = {'api_key': 'API KEY', 'hostname': 'www.malwares.com'}
    response = requests.get('https://public.api.malwares.com/v3/hostname/info', params=params)
    json_response = response.json()

    Here is the JSON used...

    {
      "result_code": "Result Code (Number)",
      "result_msg": "Result Message (String)",
      "version": "API PAGE VERSION (String)",
      "date": "API Request Time (String)",
      "hostname": "Hostname to check (String)",
      "view_count": "Hostname Search Count (Number)",
      "whois": "Hostname Whois search information (String)",
      "location": [{
        "cc" : "country code (String)",
        "cname" : "country name (String)",
        "city" : "city name (String)",
        "longitude" : "longitude (String)",
        "latitude" : "latitude (String)",
        "iplist" : "related IP list (Array)"
      }],
      "ip_history": {
        "total" : "IP number used the concerned Hostname (Number)",
        "list" : "IP list used the concerned Hostname (Array)"
      },
      "detected_url": {
        "total" : "malicious URL number which used the concerned Hostname (Number)",
        "list" : "malicious URL list which used the concerned Hostname (Array)"
      },
      "undetected_url": {
        "total" : "Normal URL number which used the concerned Hostname (Number)",
        "list" : "Normal URL list which used the concerned Hostname (Array)"
      },
      "detected_downloaded_file": {
        "total" : "malicious file number downloaded from the concerned Hostname (Number)",
        "list" : "malicious file list downloaded from the concerned Hostname (Array)"
      },
      "undetected_downloaded_file": {
        "total" : "normal file number downloaded from the concerned Hostname (Number)",
        "list" : "normal file list downloaded from the concerned Hostname (Array)"
      },
      "detected_communicating_file": {
        "total" : "malicious file number communicated with the concerned Hostname (Number)",
        "list" : "malicious file list communicated with the concerned Hostname (Array)"
      },
      "undetected_communicating_file": {
        "total" : "normal file number communicated with the concerned Hostname (Number)",
        "list" : "normal file list communicated with the concerned Hostname (Array)"
      }
    }

    Friday, August 17, 2018 11:15 AM

All replies

  • It is the way to go.

    The "params" in the python are headers on the web request.  An example function might look like:

    Public Function GetJsonResponse(apiKey As String, hostName As String, requestUrl As String) As String
        Dim request = System.Net.HttpWebRequest.CreateHttp(requestUrl)
        'these are the basic required configuration elements
        request.Headers.Item("api_key") = apiKey
        request.Host = hostName
        request.Method = Net.WebRequestMethods.Http.Get
        request.ContentType = "application/json"
    
        'this was added for testing due to a certificate error returned by the site
        request.ServerCertificateValidationCallback = Function(s As Object, certificate As Security.Cryptography.X509Certificates.X509Certificate,
          chain As Security.Cryptography.X509Certificates.X509Chain, sslPolicyErrors As Net.Security.SslPolicyErrors)
                                                          Return True
                                                      End Function
    
        'This gets the body of the response
        Using response = request.GetResponse()
            Using reader As New IO.StreamReader(response.GetResponseStream)
                Return reader.ReadToEnd
            End Using
        End Using
    End Function
    

    Note that the ServerCertificateValidationCallback was setup because the site was causing a certificate error during my testing.  This is not the best solution as it bypasses certificate errors.  You should try the code without this callback and then resolve the source of the error if it occurs for you.

    You would then call this method something like:

    RichTextBox1.Text = GetJsonResponse("(your api key goes here)", "www.malwares.com", "https://public.api.malwares.com/v3/hostname/info")
    And use your API key.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, August 17, 2018 4:25 PM
    Moderator
  • Hi and Thank you.

    When I run the code you supplied (Thank you!) I get an error on this line...

    Dim request = System.Net.HttpWebRequest.CreateHttp(requestUrl)

    WbRequest.cs not found and the program abends.

    Here is the full ABEND

    Locating source for 'f:\dd\NDP\fx\src\net\System\Net\WebRequest.cs'. (No checksum.)
    The file 'f:\dd\NDP\fx\src\net\System\Net\WebRequest.cs' does not exist.
    Looking in script documents for 'f:\dd\NDP\fx\src\net\System\Net\WebRequest.cs'...
    Looking in the projects for 'f:\dd\NDP\fx\src\net\System\Net\WebRequest.cs'.
    The file was not found in a project.
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cvt\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\msclr\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\sys\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\crt\src\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\crt\src\amd64\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\crt\src\arm\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\crt\src\concrt\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\crt\src\i386\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\crt\src\linkopts\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\crt\src\stl\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\crt\src\vccorlib\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\crt\src\vcruntime\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\atlmfc\src\mfc\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\atlmfc\src\atl\'...
    Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\atlmfc\include\'...
    The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: f:\dd\NDP\fx\src\net\System\Net\WebRequest.cs.
    The debugger could not locate the source file 'f:\dd\NDP\fx\src\net\System\Net\WebRequest.cs'.

    I googled WebRequest.cs and it brought me to a MS site that looks like th C# code for the module.

    Not sure what to do here.

    Any help would be appreciated.

    -Ron



    • Edited by NJDevils28 Friday, August 17, 2018 5:47 PM
    Friday, August 17, 2018 5:34 PM
  • ok..

    The above error was using VS2017.

    when I run the code Using VS2015 it works but I get a different error.

    {"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."}

    Would you know what this error is?  Sounds like a security issue.  I am using a valid API key if that means anything.

    Friday, August 17, 2018 6:35 PM
  • Ok - ignore all my other posts...

    I have gotten a response from the server but it appears that I'm not sending the correct parameters.

    The return I'm getting is "Invalid Parameters"

    Is there something to the we-request that I'm missing?

    Friday, August 17, 2018 7:07 PM
  • Invalid parameters is what I got when I didn't have an API key.  Are you sure the key is right?  did you double check the parameters from their API documentation?

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, August 17, 2018 9:19 PM
    Moderator