using iWareSda_QQJF.WEBAPI.JSONEntity;
|
using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Collections.Specialized;
|
using System.IO;
|
using System.Linq;
|
using System.Net;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace iWareSda_QQJF.WEBAPI
|
{
|
public class ReadErpSendToWMS
|
{
|
/// <summary>
|
/// 调用api返回json
|
/// </summary>
|
/// <param name="url">api地址</param>
|
/// <param name="jsonstr">接收参数</param>
|
/// <param name="type">类型</param>
|
/// <returns></returns>
|
public static string HttpApi(string url, string jsonstr, string type)
|
{
|
Encoding encoding = Encoding.UTF8;
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
|
|
//公有设置头文件
|
SetHeaderValue(request.Headers, "Content-Type", "application/json");
|
SetHeaderValue(request.Headers, "apiType", "client");
|
SetHeaderValue(request.Headers, "appKey", "PT20180001");
|
SetHeaderValue(request.Headers, "appSecret", "EC83335F-F695-41D5-8304-B5BFC351E5C0");
|
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
string time = Convert.ToInt64(ts.TotalMilliseconds).ToString();
|
//string time = DateTime.Now.Ticks.ToString();
|
SetHeaderValue(request.Headers, "timestamp", time);
|
Random rd = new Random();
|
long n = (long)Math.Floor((new Random()).NextDouble() * 100000000000D);
|
string num = n.ToString().PadLeft(11, '0');
|
SetHeaderValue(request.Headers, "nonce", num);
|
SetHeaderValue(request.Headers, "signature", GetSignature(time, num, jsonstr));
|
|
request.Accept = "text/html,application/xhtml+xml,*/*";
|
//request.Accept = "*/*";
|
request.ContentType = "application/json";
|
request.Method = type.ToUpper().ToString();//get或者post
|
byte[] buffer = encoding.GetBytes(jsonstr);
|
request.ContentLength = buffer.Length;
|
request.GetRequestStream().Write(buffer, 0, buffer.Length);
|
try
|
{
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
|
{
|
return reader.ReadToEnd();
|
}
|
}
|
catch (WebException ex)
|
{
|
HttpWebResponse resp = (HttpWebResponse)ex.Response;
|
|
Stream responseStream = resp.GetResponseStream();
|
|
StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
|
|
string sReturn = sr.ReadToEnd().Trim();
|
|
resp.Close();
|
|
sr.Close();
|
|
Console.WriteLine(sReturn);
|
}
|
return "";
|
}
|
|
/// <summary>
|
/// 添加物料
|
/// </summary>
|
/// <param name="item"></param>
|
public static GetJsonOfItem AdditemMethod(AddItem item)
|
{
|
string url = "http://10.103.11.236:7001/api/basicInfo/base/productInfo/add";
|
string sendJson = JsonConvert.SerializeObject(item);
|
string getInfo = HttpApi(url, sendJson, "post");
|
GetJsonOfItem getmsg = JsonConvert.DeserializeObject<GetJsonOfItem>(getInfo);
|
return getmsg;
|
}
|
|
/// <summary>
|
/// 增加任务
|
/// </summary>
|
public static GetJsonOfItem AddTaskMethod(ERPTask task)
|
{
|
string url = "http://10.103.11.236:7001/api/outbound/orderPlan/add";
|
string sendJson = JsonConvert.SerializeObject(task);
|
|
string getInfo = HttpApi(url, sendJson, "post");
|
GetJsonOfItem getmsg = JsonConvert.DeserializeObject<GetJsonOfItem>(getInfo);
|
//if (getmsg.result != false)
|
//{
|
// int a = 0;
|
//}
|
return getmsg;
|
|
}
|
|
/// <summary>
|
/// 修改任务
|
/// </summary>
|
public static GetJsonOfItem UpdateTaskMethod(ERPTask task)
|
{
|
string url = "http://10.103.11.236:7001/api/outbound/orderPlan/update";
|
string sendJson = JsonConvert.SerializeObject(task);
|
string getInfo = HttpApi(url, sendJson, "post");
|
GetJsonOfItem getmsg = JsonConvert.DeserializeObject<GetJsonOfItem>(getInfo);
|
return getmsg;
|
}
|
|
/// <summary>
|
/// 删除任务
|
/// </summary>
|
public static GetJsonOfItem DeleteTaskMethod(DeleteTask task)
|
{
|
string url = "http://10.103.11.236:7001/api/outbound/orderPlan/delete";
|
string sendJson = JsonConvert.SerializeObject(task);
|
string getInfo = HttpApi(url, sendJson, "post");
|
GetJsonOfItem getmsg = JsonConvert.DeserializeObject<GetJsonOfItem>(getInfo);
|
return getmsg;
|
}
|
|
/// <summary>
|
/// 加密
|
/// </summary>
|
/// <param name="timeStamp"></param>
|
/// <param name="nonce"></param>
|
/// <param name="paramjson"></param>
|
/// <param name="data"></param>
|
/// <returns></returns>
|
public static string GetSignature(string timeStamp, string nonce,string data)
|
{
|
|
var hash = System.Security.Cryptography.MD5.Create();
|
//拼接签名数据
|
string appkey = "PT20180001";
|
string appscret = "EC83335F-F695-41D5-8304-B5BFC351E5C0";
|
|
//timeStamp = "1615911903352";
|
//nonce = "25883581687";
|
//data="{\"createDate\":\"2021/3/16 16:32:10\"}";
|
|
data = data.Replace(".0,", ",");
|
data = data.Replace(".0}", "}");
|
var signStr = timeStamp + nonce + appkey + appscret + data;
|
//将字符串中字符按升序排序
|
var sortStr = string.Concat(signStr.OrderBy(c => c));
|
var bytes = Encoding.UTF8.GetBytes(sortStr);
|
//使用MD5加密
|
var md5Val = hash.ComputeHash(bytes);
|
//把二进制转化为大写的十六进制
|
StringBuilder result = new StringBuilder();
|
foreach (var c in md5Val)
|
{
|
result.Append(c.ToString("X2"));
|
}
|
|
|
return result.ToString().ToUpper();
|
}
|
|
/// <summary>
|
/// 设置头属性公有方法
|
/// </summary>
|
/// <param name="header"></param>
|
/// <param name="name"></param>
|
/// <param name="value"></param>
|
public static void SetHeaderValue(WebHeaderCollection header, string name, string value)
|
{
|
var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
|
if (property != null)
|
{
|
var collection = property.GetValue(header, null) as NameValueCollection;
|
collection[name] = value;
|
}
|
}
|
}
|
}
|