使用方式:
var vConsole = new VConsole();
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; | |
} | |
} | |
} |
/// <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); | |
} |
@Html.ActionLink("Action",
"Controller",
new { item1 = new EmptyParameter(), item2 = "value" });
<br>
public class EmptyParameter
{
public override string ToString()
{
return String.Empty;
}
}