1.通用方法
public class GetDataByWebApi { /// <summary> /// 通过web api获取数据的方法 /// </summary> /// <param name="url">api的url</param> /// <param name="method">请求类型,默认是get</param> /// <param name="postData">post请求所携带的数据</param> /// <returns></returns> public static string RequestData(string url, string method = "Get", string postData = null) { try { method = method.ToUpper(); //设置安全通信协议 我方公司服务器有些强制使用tls1.2的安全通信协议,所以至少包含SecurityProtocolType.Tls12 如果沒有SecurityProtocolType.Tls12设置会报错:HttpWebRequest底层连接已关闭:传送时发生意外错误 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; //创建请求实例 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //设置请求类型 request.Method = method; //设置请求消息主体的编码方法 request.ContentType = "application/json"; //POST方式處理 if (method == "POST") { //test t = test.T; //t.gaga("哟西"); //用UTF8字符集对post请求携带的数据进行编码,可防止中文乱码 byte[] byteArray = Encoding.UTF8.GetBytes(postData); //指定客户端post请求携带的数据的长度 request.ContentLength = byteArray.Length; //创建一个tream,用于写入post请求所携带的数据(该数据写入了请求体) Stream stream = request.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length); stream.Close(); } //获取请求的响应实例 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //获取读取流实体,用来以UTF8字符集读取响应流中的数据 StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), /*Encoding.UTF8*/Encoding.Default); //进行数据读取 string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); return retString; } catch (Exception ex) { //拋出異常 //throw ex; return ex.Message; } } }View Code
注:需要引用Newtonsoft.Json.dll下的Newtonsoft.Json;
2.模拟发送自定义类数据
(1)自定义类
/// <summary> /// 请求参数 /// </summary> public class ASK { /// <summary> /// 自定义数据体 /// </summary> public Dictionary<object, object> data { get; set; } /// <summary> /// 功能名 /// </summary> public string fun { get; set; } /// <summary> /// 消息id,服务端接收后会原样返回,websocket连接可用用来异步转同步用。http模式直接填0 /// </summary> public string msgid { get; set; } public ASK(Dictionary<object, object> data, string fun, string msgid) { this.data = data; this.fun = fun; this.msgid = msgid; } public ASK() { } }
(2)模拟发送
/// <summary> /// http请求方式调用web api /// </summary> /// <param name="ask">请求参数</param> /// <returns>返回调用结果</returns> public string HttpApi(ASK ask) { if (ask != null) { string jsonstr = JsonConvert.SerializeObject(ask); string result = GetDataByWebApi.RequestData("http://127.0.0.1:9912/api", "POST", jsonstr); return result; } else { return ""; } } public void PostSend() { ASK D = new ASK() { data = new Dictionary<object, object>(), fun = "get_device_list", msgid = "0" }; string jsonstr = JsonConvert.SerializeObject(D); string result = GetDataByWebApi.RequestData("http://127.0.0.1:9912/api", "POST", jsonstr);//post方式发送
//string result = GetDataByWebApi.RequestData("http://127.0.0.1:9912/api?fun=get_device_list");//Get方式发送 MessageBox.Show(result); }
标签:Web,请求,get,C#,api,Api,ASK,public,string From: https://www.cnblogs.com/Kirito-Asuna-Yoyi/p/Using_HttpApi.html