/// <summary> ///调用Url返回结果(JsonString 注:输入输出都为json格式) /// </summary> /// <typeparam name="T">T</typeparam> /// <param name="jsonStr">json字符串</param> /// <param name="url">Post的Url地址</param> /// <returns></returns> public static T JsonString <T>(this string jsonStr, string url, int outTime = OutTime) { using (WebClient client = new WebClientTimer(outTime)) { var str = JsonConvert.SerializeObject(jsonStr); client.Proxy = null; var json = client.UploadString(url, str); if (string.IsNullOrEmpty(json)) { throw new Exception("调用接口[" + url + "]失败,请求结果异常!"); } return(JsonConvert.DeserializeObject <T>(json)); } }
/// <summary> /// 调用Url返回结果(NameValueCollection) /// </summary> /// <typeparam name="T">T</typeparam> /// <param name="data"></param> /// <param name="url"></param> /// <returns></returns> public static T KVData <T>(this NameValueCollection data, string url, int outTime = OutTime) { using (WebClient client = new WebClientTimer(outTime)) { client.Proxy = null; //调用方法将键值对上传 byte[] bytes = client.UploadValues(url, data); //将字节数组的所有元素转换为一个字符串 string json = Encoding.UTF8.GetString(bytes); if (string.IsNullOrEmpty(json)) { throw new Exception("调用接口[" + url + "]失败,请求结果异常!"); } //将Json字符串转换为对象 return(JsonConvert.DeserializeObject <T>(json)); } }