private static string GetAPIServerBasePath(EnumApiServer APIServer) { string result = string.Empty; string APIUrl = string.Empty; switch (APIServer) { case EnumApiServer.Northwind: result = ConfigurationManager.AppSettings["ServiceUrl"]; break; default: throw new Exception("Unknown server."); } return(result); }
public static T GetApi <T>(EnumApiServer apiServer, string controllerName, string actionName, string getParam, bool isJson = true) { return(Api <T>(apiServer, EnumContentType.json, EnumApiMethodType.Get, controllerName + "/" + actionName, getParam, null, isJson)); }
public static T PostApi <T>(EnumApiServer apiServer, EnumContentType contentType, EnumApiMethodType apiMethodType, string controllerName, string actionName, string getParam, object parameter, bool isJson = true) { return(Api <T>(apiServer, contentType, EnumApiMethodType.Post, controllerName + "/" + actionName, getParam, parameter, isJson)); }
public static T PostApi <T>(EnumApiServer apiServer, string controllerName, string actionName, object parameter) { return(PostApi <T>(apiServer, EnumContentType.json, controllerName, actionName, null, parameter)); }
public static T Api <T>(EnumApiServer apiServer, EnumContentType contentType, EnumApiMethodType apiMethodType, string methodName, string getParam, object parameter, bool isJson = true) { if (!string.IsNullOrWhiteSpace(getParam) && !getParam.StartsWith("/")) { getParam = "/" + getParam.Trim(); } else if (string.IsNullOrWhiteSpace(getParam)) { getParam = ""; } // 整理呼叫的url string apiURL = CombinePath(GetAPIServerBasePath(apiServer), methodName) + getParam; HttpWebRequest request = HttpWebRequest.Create(apiURL) as HttpWebRequest; string PostTypeStr = ""; switch (apiMethodType) { case EnumApiMethodType.Post: PostTypeStr = WebRequestMethods.Http.Post; break; case EnumApiMethodType.Get: PostTypeStr = WebRequestMethods.Http.Get; break; case EnumApiMethodType.Put: PostTypeStr = WebRequestMethods.Http.Put; break; default: break; } request.Method = PostTypeStr; // 方法 request.KeepAlive = true; //是否保持連線 request.ContentType = GetAPIContentType(contentType); ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; //for https // 讓這個request最多等10分鐘 request.Timeout = 600000; request.MaximumResponseHeadersLength = int.MaxValue; request.MaximumAutomaticRedirections = int.MaxValue; request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; try { // 整理成呼叫的body paramter if (apiMethodType != EnumApiMethodType.Get) { string JSONParameterString = SerializeToJson <object>(parameter); byte[] bs = System.Text.Encoding.UTF8.GetBytes(JSONParameterString); using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); } } string jsonResult = ""; using (var response = request.GetResponse() as HttpWebResponse) { using (var stream = response.GetResponseStream()) { using (var reader = new StreamReader(stream)) { var temp = reader.ReadToEnd(); jsonResult = temp; //TODO:反序列化 if (temp == "" || temp == "null") { return(default(T)); } else { T result = default(T); if (isJson) { result = DeserializeJson <T>(jsonResult); } else { result = (T)Convert.ChangeType(temp, typeof(T)); } return(result); } } } } } catch (WebException webException) { if (webException.Response == null) { throw new Exception("服務無回應", webException); } using (StreamReader reader = new StreamReader(webException.Response.GetResponseStream())) { HttpWebResponse res = (HttpWebResponse)webException.Response; var pageContent = reader.ReadToEnd(); T result = JsonConvert.DeserializeObject <T>(pageContent); return(result); } } catch (Exception ex) { throw ex; } }