locked
Microsoft Translator Api partially works RRS feed

  • Question

  • Hi,I am working with "Microsoft Translator Api".At first ,it works after that occurs "The remote server returned an error: (400) Bad Request".Then i will stay for some time and try, it works ,again repeats the error.Is there any solution to recover this.Thanks..

    My Code::

     Dim clientID As String = ConfigurationManager.AppSettings("LanguageTranslator_ClientID")
                Dim clientSecret As String = ConfigurationManager.AppSettings("LanguageTranslator_Secret")
                Dim strTranslatorAccessURI As String = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"

                Dim strRequestDetails As String = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", 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

                Using 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 = Message
                    Dim headerValue As String = "Bearer " + token.access_token
                    Dim uri As String = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(txtToTranslate) + "&from=" + Fromtext + "&to=" + Totext + ""
                    ' Dim uri As String = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=" + (HttpUtility.UrlDecode(headerValue, System.Text.Encoding.Default)) + "&from=en&to=es&text=" + System.Web.HttpUtility.UrlEncode(txtToTranslate) + "&oncomplete=mycallback"

                    Dim translationWebRequest As System.Net.WebRequest = System.Net.WebRequest.Create(uri)
                    translationWebRequest.Headers.Add("Authorization", headerValue)
                    translationWebRequest.CachePolicy = New System.Net.Cache.RequestCachePolicy(System.Net.Cache.HttpRequestCacheLevel.CacheIfAvailable)
                    Using response As System.Net.WebResponse = translationWebRequest.GetResponse()
                        Dim stream As System.IO.Stream = response.GetResponseStream()
                        Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
                        Using translatedStream As System.IO.StreamReader = New System.IO.StreamReader(stream, encode)
                            Dim xTranslation As System.Xml.XmlDocument = New System.Xml.XmlDocument()
                            xTranslation.LoadXml(translatedStream.ReadToEnd)
                            webResponse.Close()
                            response.Close()
                            translatedStream.Close()
                            webRequest.Abort()

                            Return xTranslation.InnerText
                        End Using
                    End Using
                End Using


    Friday, May 5, 2017 7:43 AM

All replies

  • Your query related to Cognitive Services. For the better assistance post your query here.

    Saturday, May 6, 2017 7:44 AM