-
ApiHelper
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 class ApiHelper { public static HttpClient NewClient() { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("x-api-key", System.Configuration.ConfigurationManager.AppSettings["x-api-key"]); return client; } /// <summary> /// post方法 /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="apiUrl"></param> /// <param name="obj"></param> /// <returns></returns> public static object PostMethod(string apiUrl, string postdata) { //SSL問題 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; // //LogHelpers.WriteLog("===PostMethod===="); //LogHelpers.WriteLog("User:" + HttpContext.Current.User.Identity.Name); //LogHelpers.WriteLog(apiUrl + ":" + postdata); //var postdata = JsonConvert.SerializeObject(obj).ToString(); // var client = NewClient(); client.Timeout = TimeSpan.FromHours(1);//1小時,可自訂義 try { HttpResponseMessage response = client.PostAsync(apiUrl, new StringContent(postdata, Encoding.UTF8, "application/json")).Result; if (response.IsSuccessStatusCode) { var jsonString = response.Content.ReadAsStringAsync(); jsonString.Wait(); //List<TResult> data = JsonConvert.Deseria lizeObject<List<TResult>>(jsonString.Result); return jsonString.Result; } //可能會發生錯誤 return "error"; } catch (Exception ex) { return ex.Message; } } /// <summary> /// get方法 /// </summary> /// <param name="apiUrl"></param> /// <returns></returns> public static object GetMethod(string apiUrl) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; var client = NewClient(); // client.Timeout = TimeSpan.FromHours(1);//1小時 try { HttpResponseMessage response = client.GetAsync(apiUrl).Result; if (response.IsSuccessStatusCode) { var jsonString = response.Content.ReadAsStringAsync(); jsonString.Wait(); //List<TResult> data = JsonConvert.Deseria lizeObject<List<TResult>>(jsonString.Result); return jsonString.Result; } return "error"; } catch (Exception ex) { //Console.WriteLine(ex.Message); return ex.Message; } } } - 使用方法: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
/// <summary> /// 取得發票清單 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static object GetInvoiceList(string CompanyId) { string serviceUrl="https://slapi.com/api/Invoice/"; string apiUrl = string.Format("{0}/GetList", serviceUrl); var json=new{ CompanyIdentifier=CompanyId }; string postdata=JsonConvert.SerializeObject(json); obj apiResult = ApiHelper.GetMethod(apiUrl, postdata); } /// <summary> /// 更新訂單備註 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static object UpdateOrderRemark(string OrderId,string Remark) { string serviceUrl="https://slapi.com/api/Orders/"; string apiUrl = string.Format("{0}/UpdateOrderRemark", serviceUrl); var json=new{ RelateNumber=OrderId, MainRemark=Remark }; string postdata=JsonConvert.SerializeObject(json); obj apiResult = ApiHelper.PostMethod(apiUrl, postdata); }
2019年6月10日 星期一
[筆記]ApiHelper
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言