using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
namespace demo.DbModule
{
public class RequestData
{
public string cBarCode { get; set; }
public string cStatus { get; set; }
public string cMode { get; set; }
public string cFileName { get; set; }
}
public static class LFData
{
private static string logPath;
static LFData()
{
logPath = ConfigurationManager.AppSettings["Data"];
if(!string.IsNullOrEmpty(logPath))
{
logPath = "D:\\Data";
}
}
///
///
///
/// 产品的SN
/// Pass/Fail
/// 文件名
///
public static string Post(string barcode, string status, string filename)
{
string ret = string.Empty;
string data = "[{\"cBarCode\":\"{0}\",\"cStatus\":\"{1}\",\"cFileName\":\"{2}\"}]";
data = string.Format(data, barcode, status, filename);
try
{
string reqUrl = ConfigurationManager.AppSettings["mesUrl"];
WebRequest request = HttpWebRequest.Create(reqUrl);
request.ContentType = "application/json";
request.Method = "POST";
byte[] writeData = Encoding.UTF8.GetBytes(data);
Stream reqRequest = request.GetRequestStream();
StreamWriter sw = new StreamWriter(reqRequest);
sw.Write(writeData);
sw.Flush();
using (WebResponse reponse = request.GetResponse())
{
Stream resStream = reponse.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
ret = reader.ReadLine();
}
}
catch (Exception)
{
//记录日志
}
return ret;
}
private static void Log(string content)
{
if (!string.IsNullOrEmpty(content))
{
string dirpath = Path.Combine(logPath, DateTime.Now.ToString("yyyy-MM-dd"));
if (!System.IO.Directory.Exists(dirpath))
{
System.IO.Directory.CreateDirectory(dirpath);
}
File.AppendAllText(System.IO.Path.Combine(dirpath, "request.txt"), content);
}
}
public static string Post(string url,RequestData[] reqData)
{
string requestBody = string.Empty;
if (reqData != null)
{
requestBody = JsonConvert.SerializeObject(reqData);
}
return Post(url, null, null, requestBody);
}
public static string Post(string baseUrl, Dictionary headers, Dictionary urlParas, string requestBody=null)
{
var resuleJson = string.Empty;
Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")+" >>> :" + requestBody);
try
{
var apiUrl = baseUrl;
if (urlParas != null)
{
foreach(KeyValuePair kv in urlParas)
{
if (apiUrl.IndexOf("{" + kv.Key + "}") > -1)
{
apiUrl = apiUrl.Replace("{" + kv.Key + "}", kv.Value);
}
else
{
apiUrl += string.Format("{0}{1}={2}", apiUrl.Contains("?") ? "&" : "?", kv.Key, kv.Value);
}
}
}
var req = (HttpWebRequest)WebRequest.Create(apiUrl);
req.Method = "POST";
req.ContentType = "application/json"; //Defalt
req.Timeout = 2000;
if (!string.IsNullOrEmpty(requestBody))
{
using (var postStream = new StreamWriter(req.GetRequestStream()))
{
postStream.Write(requestBody);
}
}
if (headers != null)
{
if (headers.Keys.Any(p => p.ToLower() == "content-type"))
req.ContentType = headers.SingleOrDefault(p => p.Key.ToLower() == "content-type").Value;
if (headers.Keys.Any(p => p.ToLower() == "accept"))
req.Accept = headers.SingleOrDefault(p => p.Key.ToLower() == "accept").Value;
}
var response = (HttpWebResponse)req.GetResponse();
using(Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")))
{
resuleJson = reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
return ex.Message;
}
Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")+" <<< :" + resuleJson);
return resuleJson;// JsonConvert.DeserializeObject(resuleJson);
}
}
}