首页 > Windows开发 > 详细

工业领域C# 联合MES数据上传

时间:2021-08-20 15:59:39      阅读:14      评论:0      收藏:0      [点我收藏+]

MES系统本质是用来实现对生产现场、质量跟踪和追溯、质量控制、生产设备的管理功能,为生产部、品质保证部、工艺等部门提供车间管理信息服务。

/// <summary>
/// 创建Get方式的HTTP请求
/// </summary>
/// <param name="Url">接口地址</param>
/// <returns></returns>
public static string Get(string Url)
{
try
{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Proxy = null;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "application/json; charset=UTF-8";
request.AutomaticDecompression = DecompressionMethods.GZip;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();//服务器返回数据
//m_getstatusCode = (HttpStatusCode)Convert.ToInt32(response.StatusCode);//获取服务器状态码
m_getstatusdescription = response.StatusDescription;//获取服务器响应状态
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();

myStreamReader.Close();
myResponseStream.Close();

if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
return retString;
}
catch (Exception ex)
{
if (ex.Message.Length > 12)
{
GlobalVar.getNgResult = ex.Message.Substring(12, 3);
m_getstatusdescription = "NG";
return GlobalVar.getNgResult;
}
else
{
GlobalVar.getNgResult = "ER";
m_getstatusdescription = "NG";
return GlobalVar.getNgResult;
}

}
}


/// <summary>
/// 创建Post方式的HTTP请求
/// </summary>
/// <param name="Url"></param>
/// <param name="Data"></param>
/// <param name="Referer"></param>
/// <returns></returns>
public static string Post(string Url, string Data, string Referer)
{
try
{
string retString;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.Referer = Referer;
byte[] bytes = Encoding.UTF8.GetBytes(Data);
//request.ContentType = "application/x-www-form-urlencoded";
request.ContentType = "application/json; charset=UTF-8";
request.ContentLength = bytes.Length;
Stream myResponseStream = request.GetRequestStream();
myResponseStream.Write(bytes, 0, bytes.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
m_poststatusDescription = response.StatusDescription;//获取服务器响应状态
// m_poststatusCode = (HttpStatusCode)Convert.ToInt32(response.StatusCode);//获取服务器状态码
StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
retString = myStreamReader.ReadToEnd();

myStreamReader.Close();
myResponseStream.Close();

if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
return retString;

}
catch (Exception ex)
{

GlobalVar.postNgResult = ex.Message.Substring(12, 3);
m_poststatusDescription = "NG";
return GlobalVar.postNgResult;
}
}

}

 

工业领域C# 联合MES数据上传

原文:https://www.cnblogs.com/dumz0716/p/15166356.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!