-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendTextNoKey
More file actions
42 lines (35 loc) · 1.82 KB
/
SendTextNoKey
File metadata and controls
42 lines (35 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Text;
using Newtonsoft.Json;
//UNCOMMENT TO GET IT WORK
//class SendText
{
private static readonly string key = "API KEY HERE";
private static readonly string endpoint = "https://api.cognitive.microsofttranslator.com";
// location, also known as region.
// required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page.
private static readonly string location = "swedencentral";
public async Task<string> Translate(string translateable, string inputLang, string outputLang)
{
// Input and output languages are defined as parameters.
string route = String.Format("/translate?api-version=3.0&from={0}&to={1}&", inputLang, outputLang);
string textToTranslate = translateable;
object[] body = new object[] { new { Text = textToTranslate } };
var requestBody = JsonConvert.SerializeObject(body);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
// Build the request.
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(endpoint + route);
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
request.Headers.Add("Ocp-Apim-Subscription-Key", key);
// location required if you're using a multi-service or regional (not global) resource.
request.Headers.Add("Ocp-Apim-Subscription-Region", location);
// Send the request and get response.
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
// Read response as a string.
string result = await response.Content.ReadAsStringAsync();
return result;
}
}
}