当前位置: 首页 > news >正文

网站弹出qq聊天窗口浙江省住建厅证书查询

网站弹出qq聊天窗口,浙江省住建厅证书查询,企业信息在线查询,如何做外文网站1.WinFrom调用webapi接口方法 最近项目要在winfrom项目中调用webAPI&#xff0c;故在网上查找资料&#xff0c;找到了一个WinFrom调用webapi接口的通用方法&#xff0c;关键代码&#xff1a; #region WinFrom调用webapi接口通用方法private async Task<string> InvokeWe…

1.WinFrom调用webapi接口方法

最近项目要在winfrom项目中调用webAPI,故在网上查找资料,找到了一个WinFrom调用webapi接口的通用方法,关键代码:

 #region WinFrom调用webapi接口通用方法private async Task<string> InvokeWebapi(string url, string api, string type, Dictionary<string, string> dics){string result = string.Empty;HttpClient client = new HttpClient();client.DefaultRequestHeaders.Add("authorization", "Basic YWRtaW46cGFzc3dvcmRAcmljZW50LmNvbQ==");//basic编码后授权码client.BaseAddress = new Uri(url);client.Timeout = TimeSpan.FromSeconds(510);if (type.ToLower() == "put"){HttpResponseMessage response;//包含复杂类型if (dics.Keys.Contains("input")){if (dics != null){foreach (var item in dics.Keys){api = api.Replace(item, dics[item]).Replace("{", "").Replace("}", "");}}var contents = new StringContent(dics["input"], Encoding.UTF8, "application/json");response = client.PutAsync(api, contents).Result;if (response.IsSuccessStatusCode){result = await response.Content.ReadAsStringAsync();return result;}return result;}var content = new FormUrlEncodedContent(dics);response = client.PutAsync(api, content).Result;if (response.IsSuccessStatusCode){result = await response.Content.ReadAsStringAsync();return result;}}else if (type.ToLower() == "post"){var content = new FormUrlEncodedContent(dics);HttpResponseMessage response = client.PostAsync(api, content).Result;if (response.IsSuccessStatusCode){result = await response.Content.ReadAsStringAsync();return result;}}else if (type.ToLower() == "get"){HttpResponseMessage response = client.GetAsync(api).Result;if (response.IsSuccessStatusCode){result = await response.Content.ReadAsStringAsync();return result;}}else{return result;}return result;}#endregion   

2.应用实例

创建一个名为callWebAPI的窗体项目,在窗体加两个textBox。下面实例主要是调用webAPI的方法获取数据存为json格式,然后将json格式的数据从文件中读取出来。调用代码如下:

 private async void callWebAPI_Load(object sender, EventArgs e){#region 调用webapi并存为json格式文件string api = "";string url = "http://10.3.11.2/SimploWebApi/api/LASER_System/LASER_GetLineShowConfig?typeString=2";Dictionary<string, string> dics = new Dictionary<string, string>();Task<string> task = InvokeWebapi(url, api, "post", dics);string result = task.Result;//textBox1.Text = result;if (result != null){JObject jsonObj = null;jsonObj = JObject.Parse(result);DataInfo info = new DataInfo();info.statusCode = Convert.ToInt32(jsonObj["statusCode"]);//info.message = jsonObj["message"].ToString();if (info.statusCode == 0){JArray jlist = JArray.Parse(jsonObj["Data"].ToString());string json = JsonConvert.SerializeObject(jlist, Formatting.Indented);//File.WriteAllText("Standard.json", json);//将数据存为json格式for (int i = 0; i < jlist.Count; ++i)  //遍历JArray  {                     JObject tempo = JObject.Parse(jlist[i].ToString());textBox1.Text += tempo["line"].ToString();                    }}}#endregion#region  获取json文件数据// 读取JSON文件内容string jsonFilePath = "Standard.json";string jsons = File.ReadAllText(jsonFilePath);// 反序列化JSON到对象Standard standard = JsonConvert.DeserializeObject<Standard>(jsons);textBox2.Text = standard.line + "," + standard.linePlace;#endregion}

以上实例还需建立DataInfo类和Standard类

DataInfo类

DataInfo info = new DataInfo();

 public class DataInfo{public int statusCode { get; set; }public string message { get; set; }     }

Standard类

 Standard standard = JsonConvert.DeserializeObject<Standard>(jsons);

  public class Standard{public string line { get; set; }public string linePlace { get; set; }     }

以上实例用了json,所以需要引用Newtonsoft.Json.dll(具体引用方法见以前文章)

using Newtonsoft.Json;

Standard.json文件

{"line": "B10","linePlace": "CQ_1", }

补充:

get的传参方式

            string api = "";string sWo = "1025";          string url = "http://10.1.1.1/api/MES/API_GetLaserAOIParm?sWo=" + sWo + "";Dictionary<string, string> dics = new Dictionary<string, string>();          Task<string> task = InvokeWebapi(url, api, "post", dics);string result = task.Result;

post的传参方式

            string api = "";string sWo = "1025";          string url = "http://10.1.1.1/api/MES/API_GetLaserAOIParm";Dictionary<string, string> dics = new Dictionary<string, string>();dics = new Dictionary<string, string>{{ "sWo", sWo },            };Task<string> task = InvokeWebapi(url, api, "post", dics);string result = task.Result;

参考文献:WinForm如何调用WebApi接口_c# weiapi winform-CSDN博客

结语:本文主要记录WinFrom调用webapi接口方法及其应用实例,以上代码是本人亲测可用的,在此记录,方便查阅。

http://www.yayakq.cn/news/21026/

相关文章:

  • 做微信的网站叫什么名字福州网站建设网络公司排名
  • 原网站备案在哪深圳企业网站制作报价
  • 长治市城乡建设局网站平面广告设计案例分析
  • 网站建设报告书范文做网站设计的公司叫什么
  • 信息发布型网站建设的特点专业团队搞笑图片
  • 网站默认主页名wordpress实现文章阅读次数
  • 保定网站建设平台分析网站开发我能做什么
  • 定制型网站制作公司app开发最厉害的公司
  • 做网站 图片更好看专业制作教学课件
  • 深圳网站建设在哪里找wordpress 企业商城
  • 做公司网站需要营业执照吗html5官网首页
  • 网站系统流程图诚信企业品牌网站建设
  • 怎么样注册公司流程和费用中山做网站优化
  • 长春网站优化教程产品网络营销推广方式
  • 天远软件网站建设域名备案注销
  • 怎么做企业网站二维码扫描郑州网站建设最便宜
  • 自己做的网站被攻击了阿里云里做网站能上百度首页么
  • 微网站开发技巧电子商务网站开发的基本流程
  • 珠海网站建设创意鞍山建设集团网站
  • 广州网站推广多少钱站长seo综合查询
  • html5手机网站开发教程优创意设计平台
  • 湛江做网站优化网络服务器无响应改进措施或应对策略
  • 康保网站制作wordpress怎么修改后台登录地址
  • 重庆网站建设 公司云服务器和虚拟主机有什么区别
  • 响应式网站图片代码找建设网站公司哪家好
  • 二手书网站开发的必要性供水开发建设公司网站
  • 自考网页制作与网站建设网站建设架构优秀案例
  • 上海正规做网站公司山东省乡镇网站建设
  • 生存曲线哪个网站可以做厦门网站建设商家
  • 广州的网站建设公司php网上商城系统