欢迎使用CSDN-markdown编辑器
来源:互联网 发布:软件负载测试 编辑:程序博客网 时间:2024/06/17 05:23
C# Post方法封装
public static async Task MyPost<T>(T t,string url) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://test.com"); Dictionary<string, string> dic = new Dictionary<string, string>(); getProperties(t, dic); HttpContent content = new FormUrlEncodedContent(dic); HttpResponseMessage response = await client.PostAsync(url, content); string message = await response.Content.ReadAsStringAsync(); } public static string getProperties<T>(T t,Dictionary<string,string> myDic) { string tStr = string.Empty; if (t == null)//判空 { return tStr; } PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); if (properties.Length <= 0)//属性个数是否为0 { return tStr; } foreach (PropertyInfo item in properties) { string name = item.Name; object value = item.GetValue(t, null); if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { myDic.Add(name, value.ToString()); } } return tStr; } class Test1 { public string s1 { get; set; } public string s2 { get; set; } public float f1 { get; set; } public int i1 { get; set; } public Test1(string s1, string s2, float f1, int i1) { this.s1 = s1; this.s2 = s2; this.f1 = f1; this.i1 = i1; } }
0 0
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 使用SQL语句查询经纬度之间的距离和一定范围内的数据
- mac上配置java环境
- C# 反射通过GetCustomAttributes方法,获得自定义特性
- C语言精要总结-指针系列(一)
- 算法---索引查找
- 欢迎使用CSDN-markdown编辑器
- 纯虚函数与抽象基类
- js的date日期格式
- hibernate多对多注解配置
- matplotlib的基本用法(十一)——绘制3D图
- .Net分布式架构(二):基于Redis的Session共享
- 原生JS面向对象
- CSS实现背景图片固定滚动形成视觉差效果
- java开发环境