WinFrom调用webapi接口另一个方法及其应用实例
1.调用接口方法
代码如下:
public class WebAPI{#region WebAPI调用 public async Task<string> Call_Webapi(string Url, string Json) //url传入的是接口名称,json传入的是接口参数{string responseBody = string.Empty; //responseBody返回参数using (HttpClient client = new HttpClient()){try{// 设置请求内容类型为 JSONvar content = new StringContent(Json, Encoding.UTF8, "application/json");// 发送 POST 请求HttpResponseMessage response = await client.PostAsync(Url, content);// 确保请求成功response.EnsureSuccessStatusCode();// 读取响应内容responseBody = await response.Content.ReadAsStringAsync();}catch (HttpRequestException ex){responseBody = string.Empty;}}return responseBody;}/// <summary>/// 获取数据/// </summary>/// <param name="wo"></param>/// <returns></returns>public async Task<Result> GetWoInfo(string wo){int input = 0;int target = 0;try{string Order = wo;string URL = "http://(自己的服务器地址)/GetWOInfo"; string JSON = "{\"data\":\"" + Order + "\"}";string responseBody = await Call_Webapi(URL, JSON);//以下为解析返回的josn字符串,请先引用BIN目录里的Newtonsoft.Json.dlldynamic obj = JsonConvert.DeserializeObject(responseBody);string result = obj.Result; //读取Result返回值string Message = obj.Message;//读取Message返回值if (result == "0") //等于0时,表示工单数据正确返回,并且Resource里有数据{//解析Resource里的数据,获取PART_NO,CUST_PART_NO等你想要取的值target = obj.Resource.TARGET_QTY;input = obj.Resource.INPUT_QTY; }else //不等于0时,表示有错误,或者无数据,那么,请抛出这个错误{MessageBox.Show(Message); }}catch (Exception ex){MessageBox.Show(""+ex+""); }return new Result { target = target, input = input };}//上传数据public async Task<string> SaveTestLog_SPOT(string workNo, string line, string station, string outerBarcode, string internalBarcode, string pcbaBarcode, string cellBarcode, string carrierBarcode, string cellLot, string vendor){var result1 = "";try{string Line = line; //线别string testtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //测试时间string Order = workNo;//工单string ExternalBarcode = outerBarcode; //外码string InternalBarcode = internalBarcode;//内码;string PCB = pcbaBarcode;string CELLS = cellBarcode; //cell中间以逗号隔开 string tool = carrierBarcode;string URL = "http://(自己的服务器地址)/Api_SaveTestLog_WO"; string JSON = "{\"line\":\"" + Line + "\",\"testtime\":\"" + testtime + "\",\"Order\":\"" + Order + "\", " +"\"ExternalBarcode\":null,\"InternalBarcode\":\"" + InternalBarcode + "\",\"PCB\":\"" + PCB + "\",\"tool\":\"" + tool + "\"" +",\"cell\":\"" + CELLS + "\"}";string responseBody = await Call_Webapi(URL, JSON);//正常返回格式 :{"Result":"0","Message":"OK"}//错误返回格式:{"Result":"999","Message":"错误信息"}//以下为解析返回的josn字符串,请先引用BIN目录里的Newtonsoft.Json.dlldynamic obj = JsonConvert.DeserializeObject(responseBody);string result = obj.Result; //读取Result返回值string Message = obj.Message;//读取Message返回值if (result == "0") //等于0时,表示工单数据正确返回,并且Resource里有数据{result1 = "OK";}else //不等于0时,表示有错误,或者无数据,那么,请抛出这个错误{result1 = "NG-" + Message;}}catch (Exception ex){result1 = "NG - " + ex.Message;}if (result1 != "OK"){ MessageBox.Show("上傳MES異常:" + result1);}return result1;}#endregion}public class Result{public int target { get; set; }public int input { get; set; }}
提示:该类需要引用Newtonsoft.Json.dll文件
2.实例
创建一个winfrom项目,创建两个button和一个richTextBox,如下图:
代码如下:
public partial class Form1 : Form{public Form1(){InitializeComponent();}public WebAPI webapi = new WebAPI();private async void button1_Click(object sender, EventArgs e){//获取数据var result = await webapi.GetWoInfo("xxxxx");var target = result.target;var intput = result.input;richTextBox1.Text = "当前工单总量:"+ target + ",当前工单生产数:"+ intput + "";}private async void button2_Click(object sender, EventArgs e){//上传数据并反馈信息string workNo = "xxxxx";string line = "xxxx";string station = "xxxx";string pcba1 = "xxxx";string cell3 = "xxxx,xxxx";string cellLot = "";string vendor = "";var mesResult1 = await webapi.SaveTestLog_SPOT(workNo, line, station, "", pcba1, pcba1, cell3, "", cellLot, vendor);richTextBox1.Text = mesResult1;}}