134 lines
4.4 KiB
C#
134 lines
4.4 KiB
C#
|
|
using System;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Net;
|
|||
|
|
using System.Text;
|
|||
|
|
using Cowain.Bake.Common.Core;
|
|||
|
|
using System.Net.Http;
|
|||
|
|
|
|||
|
|
namespace Cowain.Bake.Communication.MOM
|
|||
|
|
{
|
|||
|
|
public class HttpClientHelper
|
|||
|
|
{
|
|||
|
|
public static string HttpPost1(string url, string param)
|
|||
|
|
{
|
|||
|
|
string result = "";
|
|||
|
|
if (param.Length > 7000)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
Encoding encoding = Encoding.UTF8;
|
|||
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|||
|
|
request.Method = "POST";
|
|||
|
|
request.ContentType = "application/json";
|
|||
|
|
request.Timeout = 30 * 1000;
|
|||
|
|
byte[] buffer = encoding.GetBytes(param);
|
|||
|
|
request.ContentLength = buffer.Length;
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
request.GetRequestStream().Write(buffer, 0, buffer.Length);
|
|||
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|||
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|||
|
|
{
|
|||
|
|
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
|
|||
|
|
{
|
|||
|
|
result = reader.ReadToEnd();
|
|||
|
|
reader.Close();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch(Exception ex)
|
|||
|
|
{
|
|||
|
|
LogHelper.Instance.GetCurrentClassWarn(ex.Message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string HttpPost(string url, string param)
|
|||
|
|
{
|
|||
|
|
string result = "";
|
|||
|
|
HttpResponseMessage response = null;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (HttpClient client = new HttpClient())
|
|||
|
|
{
|
|||
|
|
var data = new StringContent(param, Encoding.UTF8, "application/json");
|
|||
|
|
// 设置超时时间为30秒
|
|||
|
|
client.Timeout = TimeSpan.FromSeconds(20);
|
|||
|
|
|
|||
|
|
response = client.PostAsync(url, data).Result;
|
|||
|
|
|
|||
|
|
if (response.IsSuccessStatusCode)
|
|||
|
|
{
|
|||
|
|
result = response.Content.ReadAsStringAsync().Result;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
LogHelper.Instance.GetCurrentClassError($"HTTP POST request failed:{response.StatusCode},\n{url},\n{param}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
if (null == response)
|
|||
|
|
{
|
|||
|
|
LogHelper.Instance.GetCurrentClassError($"HTTP POST failed, 超时:{ex.Message},\n{url},\n{param}");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
LogHelper.Instance.GetCurrentClassError($"HTTP POST failed,StatusCode:{response.StatusCode},{ex.Message},\n{url},\n{param}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/*
|
|||
|
|
|
|||
|
|
public static async Task<string> HttpPost(string url, string param)
|
|||
|
|
{
|
|||
|
|
string result = "";
|
|||
|
|
if (param.Length < 7000)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
Encoding encoding = Encoding.UTF8;
|
|||
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|||
|
|
request.Method = "POST";
|
|||
|
|
request.ContentType = "application/json";
|
|||
|
|
request.Timeout = 5 * 1000;
|
|||
|
|
|
|||
|
|
byte[] buffer = encoding.GetBytes(param);
|
|||
|
|
request.ContentLength = buffer.Length;
|
|||
|
|
|
|||
|
|
var httpTask1 = Task.Run(() =>
|
|||
|
|
request.GetRequestStream().Write(buffer, 0, buffer.Length));
|
|||
|
|
var timeouttask = Task.Delay(5000);
|
|||
|
|
var completedTask = await Task.WhenAny(httpTask1, timeouttask);
|
|||
|
|
if (completedTask == timeouttask)
|
|||
|
|
{
|
|||
|
|
LogHelper.Instance.Warn("请求MOM超时");
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//request.GetRequestStream().Write(buffer, 0, buffer.Length);
|
|||
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|||
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|||
|
|
{
|
|||
|
|
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
|
|||
|
|
{
|
|||
|
|
result = reader.ReadToEnd();
|
|||
|
|
reader.Close();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return result;*/
|