HttpClient的帮助类
程序员文章站
2022-06-03 22:45:20
/// /// http请求类 /// public class HttpHelper { private HttpClient _httpClient; private string _baseIPAddress; /// 请求的基础IP,例如:http://192.168.0.33:80... ......
/// <summary> /// http请求类 /// </summary> public class httphelper { private httpclient _httpclient; private string _baseipaddress; /// <param name="ipaddress">请求的基础ip,例如:http://192.168.0.33:8080/ </param> public httphelper(string ipaddress = "") { this._baseipaddress = ipaddress; _httpclient = new httpclient { baseaddress = new uri(_baseipaddress) }; } /// <summary> /// 创建带用户信息的请求客户端 /// </summary> /// <param name="username">用户账号</param> /// <param name="pwd">用户密码,当webapi端不要求密码验证时,可传空串</param> /// <param name="uristring">the uri string.</param> public httphelper(string username, string pwd = "", string uristring = "") : this(uristring) { if (!string.isnullorempty(username)) { _httpclient.defaultrequestheaders.authorization = createbasiccredentials(username, pwd); } } /// <summary> /// get请求数据 /// /// <para>最终以url参数的方式提交</para> /// <para>yubaolee 2016-3-3 重构与post同样异步调用</para> /// </summary> /// <param name="parameters">参数字典,可为空</param> /// <param name="requesturi">例如/api/files/uploadfile</param> /// <returns></returns> public string get(dictionary<string, string> parameters, string requesturi) { if (parameters != null) { var strparam = string.join("&", parameters.select(o => o.key + "=" + o.value)); requesturi = string.concat(concaturl(requesturi), '?', strparam); } else { requesturi = concaturl(requesturi); } var result = _httpclient.getstringasync(requesturi); return result.result; } /// <summary> /// get请求数据 /// <para>最终以url参数的方式提交</para> /// </summary> /// <param name="parameters">参数字典</param> /// <param name="requesturi">例如/api/files/uploadfile</param> /// <returns>实体对象</returns> public t get<t>(dictionary<string, string> parameters, string requesturi) where t : class { string jsonstring = get(parameters, requesturi); if (string.isnullorempty(jsonstring)) return null; return jsonhelper.instance.deserialize<t>(jsonstring); } /// <summary> /// 以json的方式post数据 返回string类型 /// <para>最终以json的方式放置在http体中</para> /// </summary> /// <param name="entity">实体</param> /// <param name="requesturi">例如/api/files/uploadfile</param> /// <returns></returns> public string post(object entity, string requesturi) { string request = string.empty; if (entity != null) //将对象转换成字节码 request = jsonhelper.instance.serialize(entity); httpcontent httpcontent = new stringcontent(request); //请求的头文件 httpcontent.headers.contenttype = new mediatypeheadervalue("application/json"); return post(requesturi, httpcontent); } /// <summary> /// 提交字典类型的数据 /// <para>最终以formurlencode的方式放置在http体中</para> /// <para></para> /// </summary> /// <returns>system.string.</returns> public string postdicobj(dictionary<string, object> para, string requesturi) { dictionary<string, string> temp = new dictionary<string, string>(); foreach (var item in para) { if (item.value != null) { if (item.value.gettype().name.tolower() != "string") { temp.add(item.key, jsonhelper.instance.serialize(item.value)); } else { temp.add(item.key, item.value.tostring()); } } else { temp.add(item.key, ""); } } return postdic(temp, requesturi); } /// <summary> /// post dic数据 /// <para>最终以formurlencode的方式放置在http体中</para> /// <para></para> /// </summary> /// <returns>system.string.</returns> public string postdic(dictionary<string, string> temp, string requesturi) { httpcontent httpcontent = new formurlencodedcontent(temp); httpcontent.headers.contenttype = new mediatypeheadervalue("application/x-www-form-urlencoded"); return post(requesturi, httpcontent); } public string postbyte(byte[] bytes, string requesturl) { httpcontent content = new bytearraycontent(bytes); content.headers.contenttype = new mediatypeheadervalue("application/octet-stream"); return post(requesturl, content); } private string post(string requesturl, httpcontent content) { var result = _httpclient.postasync(concaturl(requesturl), content); return result.result.content.readasstringasync().result; } /// <summary> /// 把请求的url相对路径组合成绝对路径 /// <para></para> /// </summary> private string concaturl(string requesturl) { return new uri(_httpclient.baseaddress, requesturl).originalstring; } private authenticationheadervalue createbasiccredentials(string username, string password) { string toencode = username + ":" + password; // the current http specification says characters here are iso-8859-1. // however, the draft specification for the next version of http indicates this encoding is infrequently // used in practice and defines behavior only for ascii. encoding encoding = encoding.getencoding("utf-8"); byte[] tobase64 = encoding.getbytes(toencode); string parameter = convert.tobase64string(tobase64); return new authenticationheadervalue("basic", parameter); } }
上一篇: iOS悬浮窗口调试工具