Skip to content

Instantly share code, notes, and snippets.

@isdaviddong
Created January 25, 2019 02:44
Show Gist options
  • Select an option

  • Save isdaviddong/f9607e15f4f4946de642a4868e9e442e to your computer and use it in GitHub Desktop.

Select an option

Save isdaviddong/f9607e15f4f4946de642a4868e9e442e to your computer and use it in GitHub Desktop.
public static List<MSTranslatorTextDetectResult> Detect(string text)
{
string path = "/detect?api-version=3.0"; //偵測語系
string uri = MSTranslatorTextHost + path;
System.Object[] body = new System.Object[] { new { Text = text } };
var requestBody = JsonConvert.SerializeObject(body);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(uri);
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
request.Headers.Add("Ocp-Apim-Subscription-Key", MSTranslatorTextKey);
//取得http get結果
var response = client.SendAsync(request).Result;
var responseBody = response.Content.ReadAsStringAsync().Result;
//將取得的結果反序列化為物件
var result = JsonConvert.DeserializeObject<List<MSTranslatorTextDetectResult>>(responseBody);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment