.net接口交互
for (int i = 0; i < dtd.Rows.Count; i++) { //生成字符串 ItemModel dt = new ItemModel(); dt.action = "绑定"; dt.facility = list[j].Facility; dt.hSerial = dtID.Rows[0]["serial"].ToString(); dt.partNum = dtd.Rows[i]["partno"].ToString(); dt.qty = dtd.Rows[i]["Plan_PartsQty"].ToString(); dt.reelcut = dtd.Rows[i]["quality"].ToString(); dt.refno = list[j].RefNo; dt.Sdte = Sdte.ToString(); dt.soseq = list[j].SoSeq.ToString(); dt.slotserial = dtd.Rows[i]["Slot_Seq"].ToString(); dt.slot = dtd.Rows[i]["slot"].ToString(); dt.so = list[j].SO.ToString(); dt.stme = Stme.ToString(); dt.mtme = mtme.ToString(); dt.mdte = mdte.ToString(); itemdata.Add(dt); //************ } string url = ""; if (cboArea.Text == "4F") { url = System.Configuration.ConfigurationSettings.AppSettings["address"].ToString(); } else { url = System.Configuration.ConfigurationSettings.AppSettings["address_3D"].ToString(); } //string url = ConfigurationSettings.AppSettings["address"].ToString(); //End added at 2020/12/29 17:52 DataTable dtseial = formvimbill.GetDisksID(list[j].SoSeq); string json = JsonConvert.SerializeObject(itemdata); json = "paramInfo=" + json; LogHelper.WriteLog("开始调用:" + list[j].SO.ToString()); string result = WebApiHelper.HttpApi(url, json, "post");
交互方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Net.Security; namespace ESMT_Common { public class WebApiHelper { /// <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地址 request.Accept = "text/html,application/xhtml+xml,*/*"; request.ContentType = "application/x-www-form-urlencoded"; request.Method = type.ToUpper().ToString();//get或者post byte[] buffer = encoding.GetBytes(jsonstr); if (jsonstr != "{}"){ 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(); } } } }