| ¶Ô±ÈÐÂÎļþ |
| | |
| | | using Newtonsoft.Json; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Net; |
| | | using System.Net.Http; |
| | | using System.Text; |
| | | |
| | | namespace iWareCc |
| | | { |
| | | public class HTTPService |
| | | { |
| | | private string BaseURI = ""; |
| | | public HTTPService(string baseURI) |
| | | { |
| | | this.BaseURI = baseURI; |
| | | } |
| | | |
| | | #region GETè°ç¨webapiå
Œ
±æ¹æ³ |
| | | |
| | | /// <summary> |
| | | /// éè¿GETæ¹æ³è°ç¨HTTPæå¡ |
| | | /// </summary> |
| | | /// <typeparam name="T">å
¥åç±»å</typeparam> |
| | | /// <param name="url">æå¡å°å</param> |
| | | /// <param name="requestId">å起请æ±çè¡ä¸ºæ è¯</param> |
| | | /// <param name="timeout">è¶
æ¶æ¶é´ï¼åä½ï¼ç§ï¼é»è®¤ä¸º30ç§</param> |
| | | /// <returns></returns> |
| | | public T getContent<T>(string url, Guid requestId, int timeout = 30) |
| | | { |
| | | try |
| | | { |
| | | var client = new HttpClient(); |
| | | |
| | | client.BaseAddress = new Uri(this.BaseURI); |
| | | client.DefaultRequestHeaders.Accept.Clear(); |
| | | client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); |
| | | client.DefaultRequestHeaders.Add("RequestId", requestId.ToString()); |
| | | client.Timeout = new TimeSpan(0, 0, timeout); |
| | | |
| | | using (HttpResponseMessage response = client.GetAsync(url).Result) |
| | | { |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | var str_result = response.Content.ReadAsStringAsync().Result; |
| | | T result = JsonConvert.DeserializeObject<T>(str_result); |
| | | return result; |
| | | } |
| | | else if (response.StatusCode == HttpStatusCode.InternalServerError) |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸"); |
| | | } |
| | | else |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸{" + response.StatusCode + "}"); |
| | | } |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | throw; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿GETæ¹æ³è°ç¨HTTPæå¡ |
| | | /// </summary> |
| | | /// <param name="url">æå¡å°å</param> |
| | | /// <param name="requestId">å起请æ±çè¡ä¸ºæ è¯</param> |
| | | /// <param name="timeout">è¶
æ¶æ¶é´ï¼åä½ï¼ç§ï¼é»è®¤ä¸º30ç§</param> |
| | | /// <returns></returns> |
| | | public string getContentForString(string url, Guid requestId, int timeout = 30) |
| | | { |
| | | try |
| | | { |
| | | var client = new HttpClient(); |
| | | |
| | | client.BaseAddress = new Uri(this.BaseURI); |
| | | client.DefaultRequestHeaders.Accept.Clear(); |
| | | client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); |
| | | client.DefaultRequestHeaders.Add("RequestId", requestId.ToString()); |
| | | client.Timeout = new TimeSpan(0, 0, timeout); |
| | | |
| | | using (HttpResponseMessage response = client.GetAsync(url).Result) |
| | | { |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | var result = response.Content.ReadAsStringAsync().Result; |
| | | return result; |
| | | } |
| | | else if (response.StatusCode == HttpStatusCode.InternalServerError) |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸,response:" + JsonConvert.SerializeObject(response)); |
| | | } |
| | | else |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸{" + response.StatusCode + "},response:" + JsonConvert.SerializeObject(response)); |
| | | } |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | throw; |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | #region POSTè°ç¨webapiå
Œ
±æ¹æ³ |
| | | |
| | | /// <summary> |
| | | /// éè¿POSTæ¹æ³è°ç¨HTTPæå¡ |
| | | /// </summary> |
| | | /// <typeparam name="T">å
¥åç±»å</typeparam> |
| | | /// <typeparam name="T2">åºåç±»å</typeparam> |
| | | /// <param name="url">æå¡å°å</param> |
| | | /// <param name="parameter">å
¥å</param> |
| | | /// <param name="requestId">å起请æ±çè¡ä¸ºæ è¯</param> |
| | | /// <param name="timeout">è¶
æ¶æ¶é´ï¼åä½ï¼ç§ï¼é»è®¤ä¸º30ç§</param> |
| | | /// <returns></returns> |
| | | public T2 postContent<T, T2>(string url, T parameter, Guid requestId, int timeout = 30) |
| | | { |
| | | try |
| | | { |
| | | var client = new HttpClient(); |
| | | |
| | | client.BaseAddress = new Uri(this.BaseURI); |
| | | client.DefaultRequestHeaders.Accept.Clear(); |
| | | client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); |
| | | client.DefaultRequestHeaders.Add("RequestId", requestId.ToString()); |
| | | client.DefaultRequestHeaders.Add("Authorization", "123456"); |
| | | client.Timeout = new TimeSpan(0, 0, timeout); |
| | | |
| | | string str = JsonConvert.SerializeObject(parameter); |
| | | var httpContent = new StringContent(str, Encoding.UTF8); |
| | | httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") { CharSet = "utf-8" }; |
| | | using (HttpResponseMessage response = client.PostAsync(url, httpContent).Result) |
| | | { |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | var str_result = response.Content.ReadAsStringAsync().Result; |
| | | T2 result = JsonConvert.DeserializeObject<T2>(str_result); |
| | | return result; |
| | | } |
| | | else if (response.StatusCode == HttpStatusCode.InternalServerError) |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸"); |
| | | } |
| | | else |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸{" + response.StatusCode + "}"); |
| | | } |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | throw; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿POSTæ¹æ³è°ç¨HTTPæå¡ |
| | | /// </summary> |
| | | /// <param name="url">æå¡å°å</param> |
| | | /// <param name="parameter">å
¥å</param> |
| | | /// <param name="requestId">å起请æ±çè¡ä¸ºæ è¯</param> |
| | | /// <param name="timeout">è¶
æ¶æ¶é´ï¼åä½ï¼ç§ï¼é»è®¤ä¸º30ç§</param> |
| | | /// <returns></returns> |
| | | public string postContentForString(string url, IDictionary<string, string> parameter, Guid requestId, int timeout = 30) |
| | | { |
| | | try |
| | | { |
| | | var client = new HttpClient(); |
| | | |
| | | client.BaseAddress = new Uri(this.BaseURI); |
| | | client.DefaultRequestHeaders.Accept.Clear(); |
| | | client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); |
| | | client.DefaultRequestHeaders.Add("RequestId", requestId.ToString()); |
| | | client.Timeout = new TimeSpan(0, 0, timeout); |
| | | |
| | | string str = JsonConvert.SerializeObject(parameter); |
| | | var httpContent = new StringContent(str, Encoding.UTF8); |
| | | httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") { CharSet = "utf-8" }; |
| | | using (HttpResponseMessage response = client.PostAsync(url, httpContent).Result) |
| | | { |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | var result = response.Content.ReadAsStringAsync().Result; |
| | | return result; |
| | | } |
| | | else if (response.StatusCode == HttpStatusCode.InternalServerError) |
| | | { |
| | | return response.StatusCode.ToString(); |
| | | // throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸"); |
| | | } |
| | | else |
| | | { |
| | | return response.StatusCode.ToString(); |
| | | // throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸{" + response.StatusCode + "}"); |
| | | } |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | throw; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿POSTæ¹æ³è°ç¨HTTPæå¡ |
| | | /// </summary> |
| | | /// <param name="url">æå¡å°å</param> |
| | | /// <param name="parameter">å
¥åå符串</param> |
| | | /// <param name="requestId">å起请æ±çè¡ä¸ºæ è¯</param> |
| | | /// <param name="timeout">è¶
æ¶æ¶é´ï¼åä½ï¼ç§ï¼é»è®¤ä¸º30ç§</param> |
| | | /// <returns></returns> |
| | | public string postContentForString(string url, string parameter, Guid requestId, int timeout = 30) |
| | | { |
| | | try |
| | | { |
| | | var client = new HttpClient(); |
| | | |
| | | client.BaseAddress = new Uri(this.BaseURI); |
| | | client.DefaultRequestHeaders.Accept.Clear(); |
| | | client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); |
| | | client.DefaultRequestHeaders.Add("RequestId", requestId.ToString()); |
| | | client.Timeout = new TimeSpan(0, 0, timeout); |
| | | var ee = new { name = parameter }; |
| | | if (parameter=="") |
| | | { |
| | | parameter = ee.ToString(); |
| | | } |
| | | |
| | | var httpContent = new StringContent(parameter, Encoding.UTF8); |
| | | httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") { CharSet = "utf-8" }; |
| | | using (HttpResponseMessage response = client.PostAsync(url, httpContent).Result) |
| | | { |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | var result = response.Content.ReadAsStringAsync().Result; |
| | | return result; |
| | | } |
| | | else if (response.StatusCode == HttpStatusCode.InternalServerError) |
| | | { |
| | | return response.StatusCode.ToString(); |
| | | // throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸"); |
| | | } |
| | | else |
| | | { |
| | | return response.StatusCode.ToString(); |
| | | // throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸{" + response.StatusCode + "}"); |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | throw; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿POSTæ¹æ³è°ç¨HTTPæå¡(AGVä¸ç¨) |
| | | /// </summary> |
| | | /// <param name="url">æå¡å°å</param> |
| | | /// <param name="parameter">å
¥åå符串</param> |
| | | /// <param name="requestId">å起请æ±çè¡ä¸ºæ è¯</param> |
| | | /// <param name="timeout">è¶
æ¶æ¶é´ï¼åä½ï¼ç§ï¼é»è®¤ä¸º30ç§</param> |
| | | /// <returns></returns> |
| | | public string postContentForStringForAgv(string url, string parameter, Guid requestId, int timeout = 30) |
| | | { |
| | | try |
| | | { |
| | | var client = new HttpClient(); |
| | | |
| | | client.BaseAddress = new Uri(this.BaseURI); |
| | | client.DefaultRequestHeaders.Accept.Clear(); |
| | | client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); |
| | | client.DefaultRequestHeaders.Add("RequestId", requestId.ToString()); |
| | | client.Timeout = new TimeSpan(0, 0, timeout); |
| | | var ee = new { name = parameter }; |
| | | if (parameter == "") |
| | | { |
| | | parameter = ee.ToString(); |
| | | } |
| | | |
| | | var httpContent = new StringContent(parameter, Encoding.UTF8); |
| | | httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") { CharSet = "utf-8" }; |
| | | using (HttpResponseMessage response = client.PostAsync(url, httpContent).Result) |
| | | { |
| | | var result = response.Content.ReadAsStringAsync().Result; |
| | | return result; |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | throw; |
| | | } |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// éè¿POSTæ¹æ³è°ç¨HTTPæå¡ |
| | | /// </summary> |
| | | /// <param name="url">æå¡å°å</param> |
| | | /// <param name="parameter">å
¥åå符串</param> |
| | | /// <param name="requestId">å起请æ±çè¡ä¸ºæ è¯</param> |
| | | /// <param name="timeout">è¶
æ¶æ¶é´ï¼åä½ï¼ç§ï¼é»è®¤ä¸º30ç§</param> |
| | | /// <returns></returns> |
| | | public string postContentForStringWithToken(string url, string parameter, |
| | | Guid requestId, string token, int timeout = 30) |
| | | { |
| | | try |
| | | { |
| | | var client = new HttpClient(); |
| | | |
| | | client.BaseAddress = new Uri(this.BaseURI); |
| | | client.DefaultRequestHeaders.Accept.Clear(); |
| | | client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); |
| | | client.DefaultRequestHeaders.Add("RequestId", requestId.ToString()); |
| | | client.DefaultRequestHeaders.Add("Authorization", token.ToString()); |
| | | client.Timeout = new TimeSpan(0, 0, timeout); |
| | | |
| | | var httpContent = new StringContent(parameter, Encoding.UTF8); |
| | | httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") { CharSet = "utf-8" }; |
| | | using (HttpResponseMessage response = client.PostAsync(url, httpContent).Result) |
| | | { |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | var result = response.Content.ReadAsStringAsync().Result; |
| | | return result; |
| | | } |
| | | else if (response.StatusCode == HttpStatusCode.InternalServerError) |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸"); |
| | | } |
| | | else |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸{" + response.StatusCode + "}"); |
| | | } |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | throw; |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region PUTè°ç¨webapiå
Œ
±æ¹æ³ |
| | | |
| | | /// <summary> |
| | | /// éè¿PUTæ¹æ³è°ç¨HTTPæå¡ |
| | | /// </summary> |
| | | /// <typeparam name="T">å
¥åç±»å</typeparam> |
| | | /// <typeparam name="T2">åºåç±»å</typeparam> |
| | | /// <param name="url">æå¡å°å</param> |
| | | /// <param name="parameter">å
¥å</param> |
| | | /// <param name="requestId">å起请æ±çè¡ä¸ºæ è¯</param> |
| | | /// <param name="timeout">è¶
æ¶æ¶é´ï¼åä½ï¼ç§ï¼é»è®¤ä¸º30ç§</param> |
| | | /// <returns></returns> |
| | | public T2 putContent<T, T2>(string url, T parameter, Guid requestId, int timeout = 30) |
| | | { |
| | | try |
| | | { |
| | | var client = new HttpClient(); |
| | | |
| | | client.BaseAddress = new Uri(this.BaseURI); |
| | | client.DefaultRequestHeaders.Accept.Clear(); |
| | | client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); |
| | | client.DefaultRequestHeaders.Add("RequestId", requestId.ToString()); |
| | | client.Timeout = new TimeSpan(0, 0, timeout); |
| | | |
| | | string str = JsonConvert.SerializeObject(parameter); |
| | | var httpContent = new StringContent(str, Encoding.UTF8); |
| | | httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") { CharSet = "utf-8" }; |
| | | using (HttpResponseMessage response = client.PutAsync(url, httpContent).Result) |
| | | { |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | var str_result = response.Content.ReadAsStringAsync().Result; |
| | | T2 result = JsonConvert.DeserializeObject<T2>(str_result); |
| | | return result; |
| | | } |
| | | else if (response.StatusCode == HttpStatusCode.InternalServerError) |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸"); |
| | | } |
| | | else |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸{" + response.StatusCode + "}"); |
| | | } |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | throw; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿PUTæ¹æ³è°ç¨HTTPæå¡ |
| | | /// </summary> |
| | | /// <param name="url">æå¡å°å</param> |
| | | /// <param name="parameter">å
¥å</param> |
| | | /// <param name="requestId">å起请æ±çè¡ä¸ºæ è¯</param> |
| | | /// <param name="timeout">è¶
æ¶æ¶é´ï¼åä½ï¼ç§ï¼é»è®¤ä¸º30ç§</param> |
| | | /// <returns></returns> |
| | | public string putContentForString(string url, IDictionary<string, string> parameter, |
| | | Guid requestId, int timeout = 30) |
| | | { |
| | | try |
| | | { |
| | | var client = new HttpClient(); |
| | | |
| | | client.BaseAddress = new Uri(this.BaseURI); |
| | | client.DefaultRequestHeaders.Accept.Clear(); |
| | | client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); |
| | | client.DefaultRequestHeaders.Add("RequestId", requestId.ToString()); |
| | | client.Timeout = new TimeSpan(0, 0, timeout); |
| | | |
| | | string str = JsonConvert.SerializeObject(parameter); |
| | | var httpContent = new StringContent(str, Encoding.UTF8); |
| | | httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") { CharSet = "utf-8" }; |
| | | using (HttpResponseMessage response = client.PutAsync(url, httpContent).Result) |
| | | { |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | var result = response.Content.ReadAsStringAsync().Result; |
| | | return result; |
| | | } |
| | | else if (response.StatusCode == HttpStatusCode.InternalServerError) |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸"); |
| | | } |
| | | else |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸{" + response.StatusCode + "}"); |
| | | } |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | throw; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿POSTæ¹æ³è°ç¨HTTPæå¡ |
| | | /// </summary> |
| | | /// <param name="url">æå¡å°å</param> |
| | | /// <param name="parameter">å
¥åå符串</param> |
| | | /// <param name="requestId">å起请æ±çè¡ä¸ºæ è¯</param> |
| | | /// <param name="timeout">è¶
æ¶æ¶é´ï¼åä½ï¼ç§ï¼é»è®¤ä¸º30ç§</param> |
| | | /// <returns></returns> |
| | | public string putContentForString(string url, string parameter, |
| | | Guid requestId, int timeout = 30) |
| | | { |
| | | try |
| | | { |
| | | var client = new HttpClient(); |
| | | |
| | | client.BaseAddress = new Uri(this.BaseURI); |
| | | client.DefaultRequestHeaders.Accept.Clear(); |
| | | client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); |
| | | client.DefaultRequestHeaders.Add("RequestId", requestId.ToString()); |
| | | client.Timeout = new TimeSpan(0, 0, timeout); |
| | | |
| | | var httpContent = new StringContent(parameter, Encoding.UTF8); |
| | | httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") { CharSet = "utf-8" }; |
| | | using (HttpResponseMessage response = client.PutAsync(url, httpContent).Result) |
| | | { |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | var result = response.Content.ReadAsStringAsync().Result; |
| | | return result; |
| | | } |
| | | else if (response.StatusCode == HttpStatusCode.InternalServerError) |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸"); |
| | | } |
| | | else |
| | | { |
| | | throw new Exception("{" + this.BaseURI + "}被è°ç¨çHTTPæå¡æ¥å£{" + url + "}å
é¨åçå¼å¸¸{" + response.StatusCode + "}"); |
| | | } |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | throw; |
| | | } |
| | | } |
| | | #endregion |
| | | } |
| | | } |