You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
6.5 KiB
C#
164 lines
6.5 KiB
C#
using System.Text;
|
|
|
|
namespace SlnMesnac.WCS.Global
|
|
{
|
|
public class HttpHelper
|
|
{
|
|
public static string SendPostMessage(string ip, int port, string url, string message, string contentType = "application/Text")
|
|
{
|
|
string retsult = HttpPost("http://" + ip + ":" + port + "/" + url, message, contentType, 4, null);
|
|
return retsult;
|
|
}
|
|
|
|
public static string SendGetMessage(string ip, int port, string url)
|
|
{
|
|
string retsult = HttpGet("http://" + ip + ":" + port + "/" + url);
|
|
return retsult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发起POST同步请求
|
|
///
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="postData"></param>
|
|
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
|
|
/// <param name="headers">填充消息头</param>
|
|
/// <returns></returns>
|
|
public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 5, Dictionary<string, string> headers = null)
|
|
{
|
|
postData = postData ?? "";
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
client.Timeout = new TimeSpan(0, 0, timeOut);
|
|
if (headers != null)
|
|
{
|
|
foreach (var header in headers)
|
|
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
|
}
|
|
using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
|
|
{
|
|
if (contentType != null)
|
|
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
|
|
|
|
HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
|
|
return response.Content.ReadAsStringAsync().Result;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发起POST异步请求
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="postData"></param>
|
|
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
|
|
/// <param name="headers">填充消息头</param>
|
|
/// <returns></returns>
|
|
public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
|
|
{
|
|
postData = postData ?? "";
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
client.Timeout = new TimeSpan(0, 0, timeOut);
|
|
if (headers != null)
|
|
{
|
|
foreach (var header in headers)
|
|
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
|
}
|
|
using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
|
|
{
|
|
if (contentType != null)
|
|
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
|
|
|
|
HttpResponseMessage response = await client.PostAsync(url, httpContent);
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发起GET同步请求
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="headers"></param>
|
|
/// <param name="contentType"></param>
|
|
/// <returns></returns>
|
|
public static string HttpGet(string url, Dictionary<string, string> headers = null)
|
|
{
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
if (headers != null)
|
|
{
|
|
foreach (var header in headers)
|
|
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
|
}
|
|
HttpResponseMessage response = client.GetAsync(url).Result;
|
|
return response.Content.ReadAsStringAsync().Result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发起GET异步请求
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="headers"></param>
|
|
/// <param name="contentType"></param>
|
|
/// <returns></returns>
|
|
public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
|
|
{
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
if (headers != null)
|
|
{
|
|
foreach (var header in headers)
|
|
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
|
}
|
|
HttpResponseMessage response = await client.GetAsync(url);
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发起GET同步请求
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="headers"></param>
|
|
/// <param name="contentType"></param>
|
|
/// <returns></returns>
|
|
public static string HttpDelete(string url, Dictionary<string, string> headers = null)
|
|
{
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
if (headers != null)
|
|
{
|
|
foreach (var header in headers)
|
|
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
|
}
|
|
HttpResponseMessage response = client.DeleteAsync(url).Result;
|
|
return response.Content.ReadAsStringAsync().Result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发起GET异步请求
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="headers"></param>
|
|
/// <param name="contentType"></param>
|
|
/// <returns></returns>
|
|
public static async Task<string> HttpDeleteAsync(string url, Dictionary<string, string> headers = null)
|
|
{
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
if (headers != null)
|
|
{
|
|
foreach (var header in headers)
|
|
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
|
}
|
|
HttpResponseMessage response = await client.DeleteAsync(url);
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
}
|
|
}
|
|
} |