Created
January 25, 2019 02:44
-
-
Save isdaviddong/f9607e15f4f4946de642a4868e9e442e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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