/// <summary> /// 返回response /// </summary> /// <param name="domain"></param> /// <param name="actionName"></param> /// <param name="parameters"></param> /// <param name="header"></param> /// <returns></returns> public static IResultResponse GetResponse(string domain, string actionName, Dictionary <string, string> parameters, Dictionary <string, string> header = null) { IResultResponse result = null; try { string msg = string.Empty; string reqParameters = GetSign(actionName, parameters, FormatType.Json); NetResponse response = null; NetRequest request = new NetRequest(); request.SetTimeOut(10000); request.SetTryTimes(1); if (header != null) { foreach (KeyValuePair <string, string> kv in header) { request.AddHeader(kv.Key, kv.Value); } } if (!domain.StartsWith("http")) { domain = "http://" + domain; } string url = domain + "/Route.axd"; response = request.Post(url, reqParameters, string.Empty, out msg); if (response != null) { if (response.StatusCode == System.Net.HttpStatusCode.OK) { result = ParseResponseData(response.Content); } else { result = ResultResponse.ExceptionResult(response.StatusDescription); } } else if (!msg.IsNullOrEmpty()) { result = ResultResponse.ExceptionResult("数据接口请求错误:" + msg + "; 请求接口名: " + actionName); new Exceptions("数据接口请求错误:" + msg + "; 请求接口名: " + actionName); } else { result = ResultResponse.ExceptionResult("数据接口请求错误,请求接口名: " + actionName); } } catch (Exception ex) { result = ResultResponse.ExceptionResult(ex, ex.Message); new Exceptions(ex.Message, ex); } return(result); }
/// <summary> /// Identify the language of a selected piece of text. /// </summary> /// <param name="text">The text to detect.</param> /// <param name="appid">If the Authorization is used, leave the appid field empty else specify a string containing "Bearer" + " " + access token.</param> /// <param name="authorization">Authorization token: "Bearer" + " " + access token. Required if the appid field is not specified.</param> /// <returns>A string containing a two-character Language code for the given text.</returns> public byte[] Detect(string text, string appid = null, string authorization = null) { string queryString = ""; NetRequest request = new NetRequest(); // If the authorization has been set and appid is null. if (String.IsNullOrEmpty(authorization) && String.IsNullOrEmpty(appid)) { // Error throw throw new ArgumentNullException(nameof(appid) + " " + nameof(authorization), "The 'appid' or the 'authorization' must be set."); } // If the authorization has been set and appid is null. if (!String.IsNullOrEmpty(authorization) && String.IsNullOrEmpty(appid)) { // Add the header. request.AddHeader("Authorization", authorization); } else { queryString += "&appid=" + System.Uri.EscapeDataString(appid); } // Set the query. if ((text != null)) { queryString += "&text=" + System.Uri.EscapeDataString(text); } // Make the request. return(ProcessRequest("/" + "Detect", queryString, request)); }
/// <summary> /// Translate the text from one language to another. /// </summary> /// <param name="text">The text to translate.</param> /// <param name="to">The language code to translate the text into.</param> /// <param name="from">The language code of the translation text.</param> /// <param name="appid">If the Authorization is used, leave the appid field empty else specify a string containing "Bearer" + " " + access token.</param> /// <param name="authorization">Authorization token: "Bearer" + " " + access token. Required if the appid field is not specified.</param> /// <param name="contentType">The format of the text being translated. The supported formats are "text/plain" and "text/html". Any HTML needs to be well-formed.</param> /// <param name="category">A string containing the category (domain) of the translation. Defaults to "general".</param> /// <returns>The transalted text.</returns> public byte[] Translate(string text, string to, string from = null, string appid = null, string authorization = null, string contentType = null, string category = null) { string queryString = ""; NetRequest request = new NetRequest(); // If the authorization has been set and appid is null. if (String.IsNullOrEmpty(authorization) && String.IsNullOrEmpty(appid)) { // Error throw throw new ArgumentNullException(nameof(appid) + " " + nameof(authorization), "The 'appid' or the 'authorization' must be set."); } // If the authorization has been set and appid is null. if (!String.IsNullOrEmpty(authorization) && String.IsNullOrEmpty(appid)) { // Add the header. request.AddHeader("Authorization", authorization); } else { queryString += "&appid=" + System.Uri.EscapeDataString(appid); } // Set the query. if ((text != null)) { queryString += "&text=" + System.Uri.EscapeDataString(text); } if ((to != null)) { queryString += "&to=" + System.Uri.EscapeDataString(to); } if ((from != null)) { queryString += "&from=" + System.Uri.EscapeDataString(from); } if ((contentType != null)) { queryString += "&contentType=" + System.Uri.EscapeDataString(contentType); } if ((category != null)) { queryString += "&category=" + System.Uri.EscapeDataString(category); } // Make the request. return(ProcessRequest("/" + "Translate", queryString, request)); }