amvx86 15 Newbie Poster

Hey what's up everyone?

I'm about 3 days into hitting an extreme amount of brick walls and I'm about punch drunk. Currently I've spent 4 hours on this alone and I'm still not getting anywhere. Does anyone have some code samples that I might be able to use to put a translator together that calls on bing and or google? I've messed with some code examples, and most of them are in C# which I'm not coding in.

For Bing I have messed with this code:

           Dim BingKey = My.Computer.Registry.GetValue("hkey_local_machine\Software\Bing\Keys", "Bing", Nothing)
            Dim strTranslatedText As String = Nothing

                Dim client As New BingLang.LanguageServiceClient()
                client = New BingLang.LanguageServiceClient()

                strTranslatedText = client.Translate("AppID", RichTextBox1.Text, "de", "en")
                RichTextBox2.Text = strTranslatedText
            Catch ex As Exception
            End Try

which does not work (yes all the imports as well as the api references have been set.) I have found other code samples such as:

        Dim clientID As String = "AppID"
        Dim clientSecret As String = "KEY_CODE_HERE"
        Dim strTranslatorAccessURI As String = ""
        Dim strRequestDetails As String = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=", HttpUtility.UrlEncode(clientID), HttpUtility.UrlEncode(clientSecret))

        Dim webRequest As System.Net.WebRequest = System.Net.WebRequest.Create(strTranslatorAccessURI)
        webRequest.ContentType = "application/x-www-form-urlencoded"
        webRequest.Method = "POST"
        Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(strRequestDetails)
        webRequest.ContentLength = bytes.Length

        Using outputStream As System.IO.Stream = webRequest.GetRequestStream()
            outputStream.Write(bytes, 0, bytes.Length)
        End Using
        Dim webResponse As System.Net.WebResponse = webRequest.GetResponse()
        ' Make sure you add a reference to System.Runtime.Serialization here
        Dim AdmToken As New AdmAccessToken
        Dim serializer As System.Runtime.Serialization.Json.DataContractJsonSerializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(AdmToken.GetType())
        Dim token As AdmAccessToken = serializer.ReadObject(webResponse.GetResponseStream())
        Dim txtToTranslate As String = RichTextBox1.Text
        Dim headerValue As String = "Bearer " + token.access_token
        Dim uri As String = "" + System.Web.HttpUtility.UrlEncode(txtToTranslate) + "&from=en&to=es"

        Dim translationWebRequest As System.Net.WebRequest = System.Net.WebRequest.Create(uri)
        translationWebRequest.Headers.Add("Authorization", headerValue)
        Dim response As System.Net.WebResponse = Nothing
        response = translationWebRequest.GetResponse()
        Dim stream As System.IO.Stream = response.GetResponseStream()
        Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
        Dim translatedStream As System.IO.StreamReader = New System.IO.StreamReader(stream, encode)
        ' Be sure to add references to System.Xml and System.Xml.Linq
        Dim xTranslation As System.Xml.XmlDocument = New System.Xml.XmlDocument()

        RichTextBox2.Text = xTranslation.InnerText

And the application keeps tanking. Does anyone have an example they can share? Or, maybe even point me in the right direction? I've tried dozens of web sites, and even tried to convert C code but have been unsuccessful. I'd really appreciate it.

Highest Regards,


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.