前端js可以使用ajax、axios發出http請求
在c#中winform、控制檯等可以通過WebRequest、WebClient、HttpClient
有關三個類的效能對比大家可以自己搜一下,這裡我主要使用和封裝了HttpClient類,對於ResultDto大家可以根據所需進行修改或刪除
public class HttpHelper
{
private static readonly object LockObj = new object();
private static HttpClient client = null;
private static readonly string BASE_ADDRESS = "http://localhost:1229/";
public HttpHelper()
{
GetInstance();
}
/// <summary>
/// 製造單例
/// </summary>
/// <returns></returns>
public static HttpClient GetInstance()
{
if (client == null)
{
lock (LockObj)
{
if (client == null)
{
client = new HttpClient() {
BaseAddress = new Uri(BASE_ADDRESS)
};
}
}
}
return client;
}
/// <summary>
/// 非同步Post請求
/// </summary>
/// <param name="url">請求地址</param>
/// <param name="strJson">傳入的資料</param>
/// <returns></returns>
public async Task<string> PostAsync(string url,string strJson)
{
try
{
HttpContent content = new StringContent(strJson);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
HttpResponseMessage res = await client.PostAsync(url,content);
if (res.StatusCode == System.Net.HttpStatusCode.OK)
{
string resMsgStr = await res.Content.ReadAsStringAsync();
return resMsgStr;
}
else
{
return null;
}
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 同步Post
/// </summary>
/// <param name="url"></param>
/// <param name="strJson"></param>
/// <returns></returns>
public string Post(string url, string strJson)
{
try
{
HttpContent content = new StringContent(strJson);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
//client.DefaultRequestHeaders.Connection.Add("keep-alive");
//由HttpClient發出Post請求
Task<HttpResponseMessage> res = client.PostAsync(url, content);
if (res.Result.StatusCode == System.Net.HttpStatusCode.OK)
{
string resMsgStr = res.Result.Content.ReadAsStringAsync().Result;
return resMsgStr;
}
else
{
return null;
}
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 非同步Post請求
/// </summary>
/// <typeparam name="TResult">返回引數的資料型別</typeparam>
/// <param name="url">請求地址</param>
/// <param name="data">傳入的資料</param>
/// <returns></returns>
public async Task<TResult> PostAsync<TResult>(string url, object data)
{
try
{
var jsonData = JsonConvert.SerializeObject(data);
HttpContent content = new StringContent(jsonData);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
HttpResponseMessage res = await client.PostAsync(url, content);
if (res.StatusCode == System.Net.HttpStatusCode.OK)
{
string resMsgStr = await res.Content.ReadAsStringAsync();
var result= JsonConvert.DeserializeObject<ResultDto<TResult>>(resMsgStr);
return result != null ? result.Data : default;
}
else
{
MessageBox.Show(res.StatusCode.ToString());
return default;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return default;
}
}
/// <summary>
/// 同步Get請求
/// </summary>
/// <param name="url">請求地址</param>
/// <returns></returns>
public string Get(string url)
{
try
{
var responseString = client.GetStringAsync(url);
return responseString.Result;
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 非同步Get請求
/// </summary>
/// <param name="url">請求地址</param>
/// <returns></returns>
public async Task<string> GetAsync(string url)
{
try
{
var responseString =await client.GetStringAsync(url);
return responseString;
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 非同步Get請求
/// </summary>
/// <typeparam name="TResult">返回引數的資料</typeparam>
/// <param name="url">請求地址</param>
/// <returns></returns>
public async Task<TResult> GetAsync<TResult>(string url)
{
try
{
var resMsgStr = await client.GetStringAsync(url);
var result = JsonConvert.DeserializeObject<ResultDto<TResult>>(resMsgStr);
return result != null ? result.Data : default;
}
catch (Exception ex)
{
return default(TResult);
}
}
}
public class ResultDto<TResult>
{
public string Msg { get; set; }
public TResult Data { get; set; }
public bool Success { get; set; }
}
}
記錄我在部落格園釋出的第一篇文章,後續會把本人在csdn的文章都遷移過來
https://blog.csdn.net/hyx1229