WEB SERVICE 接口:
[WebMethod]
public string MaterialRequest(string jsonText)
{
string WorkNo;
string PN;
string PN_Name;
string Unit;
int Qty;
string Need_time="";
try
{
//jsonText格式: [{"WorkNo": "100001", "PN": "300003398", "PN_Name": "电容器,呃呃按时","Unit": "只","Qty": "5000","Need_time": "2020-1-20"},{"WorkNo": "100001", "PN": "300003397", "PN_Name": "电容器,呃呃按时","Unit": "只","Qty": "5000","Need_time": "2020-1-20"}]
JArray ja = (JArray)JsonConvert.DeserializeObject(jsonText);
//获取数值
if (ja.Count < 1) return "-1";
WorkNo = ja[0]["WorkNo"].ToString();
string sql = @"select * from WMS_KR_Task where taskno='" + WorkNo + "'";
DataTable dt = SqlHelper.ExecuteDataTable(sql);
if (dt == null) return "-1";
if (dt.Rows.Count > 0) return "1"; //已经存在
sql = "";
int counter = 0;
foreach (JObject jo in ja)
{
WorkNo = jo["WorkNo"].ToString();
PN = jo["PN"].ToString();
PN_Name = jo["PN_Name"].ToString();
Unit = jo["Unit"].ToString();
Qty = int.Parse(jo["Qty"].ToString());
Need_time = jo["Need_time"].ToString();
counter++;
sql += string.Format(@"INSERT INTO WMS_KR_Pick_Detail_From_MES(WorkNo, PN, PN_Name, Unit, Qty, Need_time) VALUES('{0}','{1}','{2}','{3}',{4},'{5}'); ", WorkNo, PN, PN_Name, Unit, Qty, Need_time);
}
sql += string.Format(@"insert into WMS_KR_TASK(TaskNo,TaskType,TaskStatus,TotalCount,ItemCount,FinishCount,RequiredTime) values('{0}',{1},{2},{3},{4},{5},'{6}');", WorkNo, (int)TaskTypeEnum.VerticalBank, (int)TaskStatusEnum.Initial, counter, counter, 0, Need_time);
SqlHelper.ExecSqlWithTrans1(sql);
return "0";
}
catch(Exception e)
{
return "-1";
}
}
C# Http Post 调用:
public void PostText()
{
string url = "http://localhost:80/WebService.asmx";
string method = "MaterialRequest";
string jsondata = "[{"WorkNo": "100001", "PN": "300003398", "PN_Name": "电容器,呃呃按时","Unit": "只","Qty": "5000","Need_time": "2020 - 1 - 20"},{"WorkNo": "100001", "PN": "300003397", "PN_Name": "电容器,呃呃按时","Unit": "只","Qty": "5000","Need_time": "2020 - 1 - 20"}]";
string result = HttpPostWebService(url, method, jsondata);
Console.WriteLine(result);
}
public string HttpPostWebService(string url, string method,string jsondata)
{
string result = string.Empty;
string param = string.Empty;
byte[] bytes = null;
Stream writer = null;
HttpWebRequest request = null;
HttpWebResponse response = null;
param = HttpUtility.UrlEncode("jsonText") + "=" + HttpUtility.UrlEncode(jsondata);
bytes = Encoding.UTF8.GetBytes(param);
request = (HttpWebRequest)WebRequest.Create(url + "/" + method);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
try
{
writer = request.GetRequestStream(); //获取用于写入请求数据的Stream对象
}
catch (Exception ex)
{
return "-1";
}
writer.Write(bytes, 0, bytes.Length); //把参数数据写入请求数据流
writer.Close();
try
{
response = (HttpWebResponse)request.GetResponse(); //获得响应
}
catch (WebException ex)
{
return "-1";
}
// web service 返回的是XML ,所有需要这么读取
#region 这种方式读取到的是一个返回的结果字符串
Stream stream = response.GetResponseStream(); //获取响应流
XmlTextReader Reader = new XmlTextReader(stream);
Reader.MoveToContent();
result = Reader.ReadInnerXml();
#endregion
// 控制器方法返回的是字符串,不管是不是XML,不能用上面的方法读取,而只能用这个方法读取返回的字符。
#region 这种方式读取到的是一个Xml格式的字符串
//StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
//result = reader.ReadToEnd();
#endregion
response.Dispose();
response.Close();
Reader.Dispose();
Reader.Close();
stream.Dispose();
stream.Close();
return result;
}
注意:下一行是否注释都能正常执行
下面是C#环境的测试界面:
如果按照上面的URL:http://localhost:6311/WebService.asmx?op=MaterialRequest 执行错误。
URL必须用: http://localhost:80/WebService.asmx/MaterialRequest