日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

微软服务器异常是什么原因,远程服务器返回异常:(400)异常的请求,微软的翻译接口...

發布時間:2024/5/8 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微软服务器异常是什么原因,远程服务器返回异常:(400)异常的请求,微软的翻译接口... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

當前位置:我的異常網? C#???遠程服務器返回異常:(400)異常的請求,微軟的翻譯接

遠程服務器返回異常:(400)異常的請求,微軟的翻譯接口

www.myexceptions.net??網友分享于:2015-08-26??瀏覽:891次

遠程服務器返回錯誤:(400)錯誤的請求,微軟的翻譯接口

最近在用微軟的翻譯接口,但也是報 遠程服務器返回錯誤:(400)錯誤的請求, 問題出在下面我標示為紅色的代碼,請高手指點一下錯在哪了

static void Main(string[] args)

{

AdmAccessToken admToken;

string headerValue;

//Get Client Id and Client Secret from https://datamarket.azure.com/developer/applications/

//Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx)

string clientID = "hongxuebiaoTranslator";

string clientSecret = "KJn2ZfUPAAruDJECbpg7MrzsNWGiHtcmjmeQJYkD6hw";

AdmAuthentication admAuth = new AdmAuthentication(clientID, clientSecret);

try

{

admToken = admAuth.GetAccessToken();

// Create a header with the access_token property of the returned token

headerValue = "Bearer " + admToken.access_token;

TranslateMethod(headerValue);

}

catch (WebException e)

{

ProcessWebException(e);

Console.WriteLine("Press any key to continue...");

Console.ReadKey(true);

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

Console.WriteLine("Press any key to continue...");

Console.ReadKey(true);

}

}

private static void TranslateMethod(string authToken)

{

string text = "Use pixels to express measurements for padding and margins.";

string from = "en";

string to = "de";

string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(text) + "&from=" + from + "&to=" + to;

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);

httpWebRequest.Headers.Add("Authorization", authToken);

WebResponse response = null;

try

{

response = httpWebRequest.GetResponse();

using (Stream stream = response.GetResponseStream())

{

System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));

string translation = (string)dcs.ReadObject(stream);

Console.WriteLine("Translation for source text '{0}' from {1} to {2} is", text, "en", "de");

Console.WriteLine(translation);

}

Console.WriteLine("Press any key to continue...");

Console.ReadKey(true);

}

catch

{

throw;

}

finally

{

if (response != null)

{

response.Close();

response = null;

}

}

}

private static void ProcessWebException(WebException e)

{

Console.WriteLine("{0}", e.ToString());

// Obtain detailed error information

string strResponse = string.Empty;

using (HttpWebResponse response = (HttpWebResponse)e.Response)

{

using (Stream responseStream = response.GetResponseStream())

{

using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))

{

strResponse = sr.ReadToEnd();

}

}

}

Console.WriteLine("Http status code={0}, error message={1}", e.Status, strResponse);

}

}

[DataContract]

public class AdmAccessToken

{

[DataMember]

public string access_token { get; set; }

[DataMember]

public string token_type { get; set; }

文章評論

總結

以上是生活随笔為你收集整理的微软服务器异常是什么原因,远程服务器返回异常:(400)异常的请求,微软的翻译接口...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。