176 lines
7.1 KiB
C#
176 lines
7.1 KiB
C#
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Net;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
|
||
namespace C_Windows_1
|
||
{
|
||
public class MESInfo
|
||
{
|
||
|
||
//上传至mes系统
|
||
public static string MESInformation(string url, string appId, string appKey, string method, string dept, string type, string model,
|
||
string station, string timestamp, string sn, string result, string inflationpressuremax, string inflationpressuremin, string inflationpressure,
|
||
string balancepressuremax, string balancepressuremin, string balancepressure, string leakmax, string leakmin, string leakage)
|
||
{
|
||
try
|
||
{
|
||
Dictionary<String, Object> map = new Dictionary<string, object>();
|
||
|
||
map["dept"] = dept;
|
||
map["type"] = type;
|
||
map["model"] = model;
|
||
map["station"] = station;
|
||
map["timestamp"] = timestamp;
|
||
map["sn"] = sn;
|
||
map["result"] = result;
|
||
map["inflationPressureMax"] = inflationpressuremax;
|
||
map["inflationPressureMin"] = inflationpressuremin;
|
||
map["inflationPressure"] = inflationpressure;
|
||
map["balancePressureMax"] = balancepressuremax;
|
||
map["balancePressureMin"] = balancepressuremin;
|
||
map["balancePressure"] = balancepressure;
|
||
map["leakMax"] = leakmax;
|
||
map["leakMin"] = leakmin;
|
||
map["leakage"] = leakage;
|
||
String body = JsonConvert.SerializeObject(map);
|
||
//Form1.f1.textBox1.Text = body;
|
||
String signStr = appId + body + appKey; // 拼接加密字符串
|
||
String sign = GenerateMD5(signStr).ToUpper();
|
||
|
||
Dictionary<String, Object> header = new Dictionary<string, object>();
|
||
header["appid"] = appId;
|
||
header["sign"] = sign;
|
||
header["method"] = method;
|
||
Dictionary<String, Object> data = new Dictionary<string, object>();
|
||
data["header"] = header;
|
||
data["body"] = body;//放入dictionary中
|
||
|
||
//Form1.f1.textBox3.Text = JsonConvert.SerializeObject(data);//data又被转了一次Jason
|
||
|
||
//string dataMap= JsonConvert.SerializeObject(data)
|
||
//String base64 = Base64.encodeBase64String(Utils.toJsonString(data).getBytes());
|
||
byte[] bytedata = System.Text.Encoding.Default.GetBytes(JsonConvert.SerializeObject(data));
|
||
string base64 = Convert.ToBase64String(bytedata);//
|
||
|
||
Dictionary<String, Object> dataMap = new Dictionary<string, object>();
|
||
dataMap["data"] = base64;
|
||
// Form1.f1.textBox1.Text = base64;
|
||
|
||
|
||
//日志
|
||
Log log = new Log();
|
||
string datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
log.Logmsg(datetime + " MESSend: " + JsonConvert.SerializeObject(data));
|
||
|
||
//发送数据
|
||
string url2 = url + "?" + "data=" + base64;
|
||
string ss = HttpPost(url2, "");
|
||
|
||
//string ss = HttpGet("http://xms.be.test.xiao.com/xmsapi");
|
||
//日志
|
||
string datetime2 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
log.Logmsg(datetime + " MESReceive: " + ss + "\n");
|
||
|
||
return ss;
|
||
//return "";
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return ex.Message;
|
||
}
|
||
|
||
}
|
||
|
||
//MD5加密sign参数
|
||
public static string GenerateMD5(string txt)
|
||
{
|
||
using (MD5 mi = MD5.Create())
|
||
{
|
||
byte[] buffer = Encoding.Default.GetBytes(txt);
|
||
//开始加密
|
||
byte[] newBuffer = mi.ComputeHash(buffer);
|
||
StringBuilder sb = new StringBuilder();
|
||
for (int i = 0; i < newBuffer.Length; i++)
|
||
{
|
||
sb.Append(newBuffer[i].ToString("x2"));//二位十六进制,x就是转成十六进制
|
||
}
|
||
return sb.ToString();
|
||
}
|
||
}
|
||
|
||
|
||
|
||
//调用API
|
||
public static string HttpPost(string url, string body)
|
||
{
|
||
|
||
Encoding encoding = Encoding.UTF8;
|
||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||
request.Method = "POST";
|
||
request.Accept = "text/html, application/xhtml+xml, */*";
|
||
request.ContentType = "application/json";
|
||
|
||
byte[] buffer = encoding.GetBytes(body);
|
||
request.ContentLength = buffer.Length;
|
||
request.GetRequestStream().Write(buffer, 0, buffer.Length);
|
||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
|
||
{
|
||
return reader.ReadToEnd();
|
||
}
|
||
|
||
|
||
}
|
||
|
||
//static CookieContainer cookie = new CookieContainer();
|
||
|
||
////调用API
|
||
//public static string HttpPost2(string Url, Dictionary<String, Object> postDataStr)
|
||
//{
|
||
|
||
// //Encoding encoding = Encoding.UTF8;
|
||
// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
|
||
// request.Method = "POST";
|
||
// request.Accept = "text/html, application/xhtml+xml, */*";
|
||
// request.ContentType = "application/x-www-form-urlencoded";
|
||
// request.CookieContainer = cookie;
|
||
// Stream myRequestStream = request.GetRequestStream();
|
||
// StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
|
||
// myStreamWriter.Write(postDataStr);
|
||
// myStreamWriter.Close();
|
||
|
||
// HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||
// response.Cookies = cookie.GetCookies(response.ResponseUri);
|
||
// Stream myResponseStream = response.GetResponseStream();
|
||
// StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
|
||
// string retString = myStreamReader.ReadToEnd();
|
||
// myStreamReader.Close();
|
||
// myResponseStream.Close();
|
||
// return retString;
|
||
|
||
//}
|
||
|
||
//public static string HttpGet(string url)
|
||
//{
|
||
// //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
||
// Encoding encoding = Encoding.UTF8;
|
||
// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||
// request.Method = "GET";
|
||
// request.Accept = "text/html, application/xhtml+xml, */*";
|
||
// request.ContentType = "application/json";
|
||
|
||
// HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||
// using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
|
||
// {
|
||
// return reader.ReadToEnd();
|
||
// }
|
||
//}
|
||
|
||
|
||
}
|
||
}
|