VB.NET using the Cloudflare API

Asphyxia

Owner
Administrator
Apr 25, 2015
1,845
2
2,199
327
Firstly, I highly recommend for the sake of simplicity that you use a Windows Forms type of application. I mean you could go the console route, but I am not going to be showing this here - just the Windows Forms way.

Create a new VB.NET Windows Forms App..

1569412046004.png

-> Next

Name the project CiphersCloud.

-> Create

1569416591911.png

Make two textbox inputs and a button.

Name the first textbox txtEmail and the second one txtGlobalAPI!

Name the button btnAuth, the text can be "Authenticate"

Go to Tools -> NuGet Package Manager -> Manage NuGet Packages...

Top left, click "Browse" and search for "newtonsoft.json"

1569416898362.png

Install this JSON package into your solution.

Add this function into your project code:
Code:
Public Function WRequest(URL As String, method As String, POSTdata As String) As String
        Dim responseData As String = ""
        Try
            Dim cookieJar As New Net.CookieContainer()
            Dim hwrequest As Net.HttpWebRequest = Net.WebRequest.Create(URL)
            'hwrequest.CookieContainer = cookieJar
            hwrequest.Accept = "*/*"
            hwrequest.AllowAutoRedirect = True
            hwrequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
            hwrequest.Timeout = 60000
            hwrequest.ContentType = "application/json"
            hwrequest.Headers.Add("X-Auth-Email: " + txtEmail.Text)
            hwrequest.Headers.Add("X-Auth-Key: " + txtGlobalAPI.Text)
            hwrequest.Method = method
            If hwrequest.Method = "POST" Then
                hwrequest.ContentType = "application/json"
                Dim encoding As New Text.ASCIIEncoding() 'Use UTF8Encoding for XML requests
                Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)
                hwrequest.ContentLength = postByteArray.Length
                Dim postStream As IO.Stream = hwrequest.GetRequestStream()
                postStream.Write(postByteArray, 0, postByteArray.Length)
                postStream.Close()
            End If
            Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
            If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
                Dim responseStream As IO.StreamReader =
        New IO.StreamReader(hwresponse.GetResponseStream())
                responseData = responseStream.ReadToEnd()
            End If
            hwresponse.Close()
        Catch e As Exception
            responseData = "An error occurred: " & e.Message
        End Try
        Return responseData
    End Function

Insert this import into the very top of your project:
Code:
Imports Newtonsoft.Json.Linq

... great now just make your Authenticate button do this:

Code:
Private Sub BtnAuth_Click(sender As Object, e As EventArgs) Handles btnAuth.Click
        Dim wData As String
        wData = WRequest("https://api.cloudflare.com/client/v4/zones", "GET", "")
        txtResult.Text = wData
    End Sub

Make sure to add a last textbox, make it multiline (True) and name that "txtResult"...

Then for parsing JSON here is an example:
Code:
Dim json As JObject = JObject.Parse(Me.txtResult.Text)
        'MsgBox(json.SelectToken("result_info").SelectToken("page"))
        'MsgBox(json.ToString)
        'MsgBox(json.SelectToken("result[2]").SelectToken("name"))

        For x = 0 To (json.SelectToken("result").Count - 1)
            MsgBox(json.SelectToken("result[" & x & "]").SelectToken("name"))
        Next

        'MsgBox(json.SelectToken("result").Count)

Please ask any questions you may have...

Also, if you want to use Console you could use this class as inspiration.

Code:
' Title: Cloudflare.vb
' Author: @Yizack
' Description: Visual Basic class for purge files programmatically with the Cloudflare API.
' Date: 2019/05/15

Imports System.Net
Imports System.Text

Public Class Cloudflare
    ' Example use:
    ' PurgeFile("https://wwww.example.com/image/cat.jpg", "[email protected]", "YOUR API KEY", "YOUR ZONE KEY")
  
    ' Example use on another class:
    ' Cloudflare.PurgeFile("https://wwww.example.com/image/cat.jpg", "[email protected]", "YOUR API KEY", "YOUR ZONE KEY")
  
    ' Console success output: {"result":{"id":"YOUR ZONE KEY"},"success":true,"errors":[],"messages":[]}
  
    Public Sub PurgeFile(ByVal FILE_URL As String, ByVal EMAIL As String,ByVal API_KEY As String, ByVal ZONE_KEY As String)
        Dim webClient As New WebClient()
        Dim resByte As Byte()
        Dim resString As String
        Dim data As Byte() = Encoding.Default.GetBytes("{" & Chr(34) & "files" & Chr(34) & ":[" & Chr(34) & FILE_URL & Chr(34) & "]}")
        Try
            webClient.Headers("X-Auth-Email") = EMAIL
            webClient.Headers("X-Auth-Key") = API_KEY
            webClient.Headers("content-type") = "application/json"
            resByte = webClient.UploadData("https://api.cloudflare.com/client/v4/zones/" & ZONE_KEY & "/purge_cache", "POST", data)
            resString = Encoding.Default.GetString(resByte)
            Console.WriteLine(resString)
            webClient.Dispose()
        Catch ex As Exception
             Console.WriteLine(ex.Message)
        End Try
    End Sub
End Class

I am also attaching an icon you could use!
 

Attachments

  • icon.zip
    9.9 KB · Views: 4
Last edited:
Top