我想向
api.cognitive.microsofttranslator.com/translate
发送http请求并发送一些文本进行翻译,但它不起作用。
我尝试这个请求:
Public Sub translate()
Dim strUrl As String
Dim params As String
Dim strResponseHeaders As String
Dim allResponseHeader As String
Dim strResponse As String
Dim body As String
Dim phrase As String: phrase = "some text to translate"
Dim target As String: target = "mk"
Dim some_key as string : some_key = "somekey"
dim location as string: location = "west..."
body = "[{""text"":""" & phrase & """}]"
strUrl = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0"
params = "&from=en&to=" & target
Dim hReq As Object
Set hReq = CreateObject("MSXML2.XMLHTTP")
With hReq
.Open "POST", strUrl & params, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Content-Type", "charset=UTF-8"
.setRequestHeader "Ocp-Apim-Subscription-Key", some_key
.setRequestHeader "Ocp-Apim-Subscription-Region", location
.setRequestHeader "Content-Length", Len(body)
.send body
strResponseHeaders = .StatusText
strResponse = .responseText
allResponseHeader = .getAllResponseHeaders
'translate = hReq.ResponseText
End With
End Sub
我在
.send body
收到错误
==参数不正确
我尝试使用批处理文件中的curl 请求。如果我从 cmd 运行它工作正常:
set "traceId=%random%%random%%random%"
set "endpoint=https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=mk"
set "requestData=[{""text"":""the price is""}]"
curl --location "%endpoint%" ^
--header "Ocp-Apim-Subscription-Key: %key%" ^
--header "Ocp-Apim-Subscription-Region: %region%" ^
--header "X-ClientTraceId: %traceId%" ^
--header "Content-Type: application/json" ^
--header "Content-Type: charset=UTF-8" ^
--data "%requestData%"
从 MS Access 中的 VBA 向 Azure 转换器发送 HTTP 请求
您可以使用以下代码在 MS Access 中使用从
HTTP
向 Azure 转换器发送 vba
请求。
代码:
Imports System.IO
Imports System.Net
Imports System.Text
Imports Newtonsoft.Json
Module Program
Sub Main(args As String())
Console.OutputEncoding = Encoding.UTF8
Dim sourceLanguage As String = "en"
Dim targetLanguage As String = "mk"
Dim translatedText As String = Translator.TranslateText("Welcome to my world!", sourceLanguage, targetLanguage)
Console.WriteLine(translatedText)
End Sub
End Module
Public Class Translator
Private Shared subscriptionKey As String = "<Your api key>"
Private Shared location As String = "<Your location>"
Public Shared Function TranslateText(ByVal text As String, ByVal sourceLanguage As String, ByVal targetLanguage As String) As String
Dim endpoint As String = $"https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from={sourceLanguage}&to={targetLanguage}"
Dim requestBody As Object = New Object() {New With {Key .Text = text}}
Dim requestBodyJson As String = JsonConvert.SerializeObject(requestBody)
Dim request As HttpWebRequest = CType(WebRequest.Create(EndPoint), HttpWebRequest)
request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey)
request.Headers.Add("Ocp-Apim-Subscription-Region", location)
request.ContentType = "application/json charset=UTF-8"
request.Method = "POST"
Using streamWriter As StreamWriter = New StreamWriter(request.GetRequestStream())
streamWriter.Write(requestBodyJson)
End Using
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Using streamReader As StreamReader = New StreamReader(response.GetResponseStream(), Encoding.UTF8)
Dim responseJson As String = streamReader.ReadToEnd()
Dim result As Object() = JsonConvert.DeserializeObject(Of Object())(responseJson)
Dim translation As String = result(0)("translations")(0)("text").ToString()
Return translation
End Using
End Function
End Class
上述代码用于使用 Azure 认知服务将文本从一种语言翻译为另一种语言。它使用适当的标头和请求正文向 Translator API 端点构造一个
HTTP
请求,并解析响应以提取翻译后的文本。
此外,您还需要使用
Newtonsoft.Json
库来处理 serialize
和 deserialize
JSON 数据。
输出:
Добредојдовте во мојот свет!
参考: