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.

313 lines
11 KiB
C#

using Newtonsoft.Json;
using System.Net;
using System.Net.Http;
using System.Text;
namespace HttpTest
{
public class HttpHelper
{
public static string Get(string serviceAddress)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
public static async Task<string> GetAsync(string serviceAddress)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = await myStreamReader.ReadToEndAsync();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
public static string Post111(string url, string jsonContent)
{
HttpClient _client = new HttpClient();
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = _client.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsStringAsync().Result;
}
else
{
throw new HttpRequestException($"POST request to {url} failed with status code {response.StatusCode}");
}
}
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, 30, null);
return retsult;
}
public static string SendGetMessage(string ip, int port, string url)
{
string retsult = HttpGet("http://" + ip + ":" + port + "/" + url);
return retsult;
}
/// <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;
}
}
public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
{
try
{
postData = postData ?? "";
string result = Post("http://localhost:5001/wcs/RecieveRcs/AgvTaskComplete", "");
return result;
//using (HttpClient client = new HttpClient())
//{
// 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;
// }
//}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
public static string Post(string serviceAddress, string strContent = null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "POST";
request.ContentType = "application/json";
//判断有无POST内容
if (!string.IsNullOrWhiteSpace(strContent))
{
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
{
dataStream.Write(strContent);
dataStream.Close();
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
return retString;
}
//public static void TestConnectionAsync(HttpClient client,string url)
//{
// try
// {
// HttpResponseMessage response = client.GetAsync(url).Result;
// if (response.IsSuccessStatusCode)
// {
// Console.WriteLine($"Success: Connected to {url}");
// }
// else
// {
// Console.WriteLine($"Failure: Unable to connect to {url}. Status code: {response.StatusCode}");
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine($"Exception: {ex.Message}");
// }
//}
//public static string Post(string serviceAddress, string strContent = null)
//{
// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
// request.Method = "POST";
// request.ContentType = "application/Json";
// //判断有无POST内容
// if (!string.IsNullOrWhiteSpace(strContent))
// {
// using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
// {
// dataStream.Write(strContent);
// dataStream.Close();
// }
// }
// HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// string encoding = response.ContentEncoding;
// if (encoding.Length < 1)
// {
// encoding = "UTF-8"; //默认编码
// }
// StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
// string retString = reader.ReadToEnd();
// return retString;
//}
//public static string Post1(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
//{
// postData = postData ?? "";
// using (HttpClient client = new HttpClient())
// {
// 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;
// }
// }
//}
public static async Task<string> PostAsync(string serviceAddress, string strContent = null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "POST";
request.ContentType = "application/Json";
//判断有无POST内容
if (!string.IsNullOrWhiteSpace(strContent))
{
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
{
dataStream.Write(strContent);
dataStream.Close();
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = await reader.ReadToEndAsync();
return retString;
}
/// <summary>
/// Agv任务是否完成的接口
/// </summary>
/// <param name="orderId"></param>
/// <returns></returns>
public static bool JudgeAgvTaskComplete()
{
try
{
string url = $"http://192.168.1.103:5001/wcs/RecieveRcs/callMaterial";
string v = HttpHelper.SendPostMessage("127.0.0.1", 5001, "wcs/RecieveRcs/AgvTaskComplete", "", "application/Json");
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 通知Agv小车送料
/// </summary>
/// <param name="orderId"></param>
/// <returns></returns>
public static bool SendAgvTask(string orderId)
{
try
{
// 创建请求对象
var request = new
{
RawOutstockId = orderId
};
// 序列化为 JSON 字符串
string jsonData = JsonConvert.SerializeObject(request);
string url = $"http://127.0.0.1:5001/wcs/RecieveRcs/callMaterial";
var Result = Post(url, jsonData);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}