好像UWP里面还是httpClient好用些,添加Headers很方便.但是HttpContent要Json数据,想了半天.
public static async Task<string> Bing_Translate(string from, string to, string content) { string token = await GetBingToken(); var url = new Uri($"https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from={from}&to={to}", UriKind.Absolute); var httpClient = new System.Net.Http.HttpClient(); tContent tc = new tContent { text = content }; List<tContent> tcs = new List<tContent> { tc}; string json = JsonConvert.SerializeObject(tcs); Debug.WriteLine(json); HttpContent httpContent = new StringContent(json); httpContent.Headers.ContentType= new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); Debug.WriteLine(httpContent.ToString()); var reponse = await httpClient.PostAsync(url, httpContent); String result = await reponse.Content.ReadAsStringAsync(); return result; }各种尝试后,还是需要自己建立一个与json对应的对象,然后序列化成为string,还以为httpContent可以自己完成这一步...
然后变为字符串上传.有点无语,bing的格式要求有点恶心啊.