Word-list API calls in VB.Net

lkneebone489lkneebone489 Member
edited March 2017 in General

Hello. My language of choice is VB.Net. I wrote some code to use the API's Wordlist feature and thought maybe others could benefit from this.

What I did was create a new Web Application in Visual Studio, added a "web form" to the project. The HTML in my .aspx file looks something like this...

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="OxfordAPI._Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="status" runat="server"></div>
    <div id="main" runat="server"></div>
    </form>
</body>
</html>

And my VB in my .aspx.vb file looks like this...

Imports System
Imports System.Net
Imports System.IO
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        Dim app_id As String = "YourAppID"
        Dim app_key As String = "YourAppKey"

        'the following line is the parameters for the WordList call (edit as needed)
        Dim params As String = "lexicalCategory=noun?exclude=grammaticalFeatures=abbreviation&word_length=%3C6&exact=false"

        'Put the whole URL together
        Dim url = "https://od-api.oxforddictionaries.com:443/api/v1/wordlist/en/" & params


        ' Create a request for the URL. 
        Dim request As WebRequest = WebRequest.Create(url)

        'set the credentials.
        request.Headers.Add("app_id:" & app_id)
        request.Headers.Add("app_key:" & app_key)

        ' Get the response.
        Dim response As WebResponse = request.GetResponse()

        ' Display the status on the page
        status.InnerHtml = "STATUS: " & CType(response, HttpWebResponse).StatusDescription

        ' Get the stream containing content returned by the server.
        Dim dataStream As Stream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim responseFromServer As String = reader.ReadToEnd()


        'as the results come in the form of Json, we need to extract all the words using a loop
        Dim rootResults = JObject.Parse(responseFromServer)
        Dim results As List(Of JToken) = rootResults.Children().ToList

        Dim newHTML As String = ""

        For Each item As JProperty In results
            item.CreateReader()
            Select Case item.Name
                Case "results"
                    For Each subitem As JObject In item.Values
                        newHTML = newHTML & "<br />" & subitem("word").ToString
                    Next
            End Select
        Next



        ' Display the content.
        main.InnerHtml = newHTML

        ' Clean up the streams and the response.
        reader.Close()
        response.Close()

    End Sub

End Class

Keep in mind that this code can easily be converted to C#, if that is what you prefer. If you want the VB code for other API end points, the above code could be modified to achieve it.

I hope someone finds this useful :)

Comments

Sign In or Register to comment.