public class New@R_403_6822@ClIEnt2 { public enum BodyFormat { Formed,Json } public BodyFormat BodyType; public string Method; public Uri Uri; public EnCoding ContentEnCoding; public Dictionary<string,object> Bodys; public Dictionary<string,string> headers; public Dictionary<string,string> querys; public int Timeout; private New@R_403_6822@ClIEnt2() { BodyType = BodyFormat.Formed; Method = "GET"; ContentEnCoding = EnCoding.UTF8; Bodys = new Dictionary<string,object>(); headers = new Dictionary<string,string>(); querys = new Dictionary<string,string>(); ServicePointManager.Expect100Continue = false; Timeout = 60000; } public New@R_403_6822@ClIEnt2(string uri) : this() { Uri = new Uri(uri); } public @R_403_6822@WebRequest @R_403_6822@WebRequest; public string UriWithquery { get { return Uri + "?" + Util.ParsequeryString(querys); } } public JObject PostAction() { @R_403_6822@WebRequest = (@R_403_6822@WebRequest)WebRequest.Create(UriWithquery); @R_403_6822@WebRequest.Method = Method; @R_403_6822@WebRequest.ReaDWriteTimeout = 30000; foreach (var header in headers) { @R_403_6822@WebRequest.headers.Add(header.Key,header.Value); } byte[] body = null; switch (BodyType) { case BodyFormat.Formed: { var bodydata = Bodys.Select(pair => pair.Key + "=" + Util.UrIEncode(pair.Value.ToString())) .DefaultIfEmpty("") .Aggregate((a,b) => a + "&" + b); body = ContentEnCoding.GetBytes(bodydata); @R_403_6822@WebRequest.ContentType = "application/x-www-form-urlencoded"; break; } case BodyFormat.Json: { var bodydata = JsonConvert.SerializeObject(Bodys); body = ContentEnCoding.GetBytes(bodydata); @R_403_6822@WebRequest.ContentType = "application/Json"; break; } } if (body != null && Bodys.Count > 0) { var stream = @R_403_6822@WebRequest.GetRequestStream(); stream.Write(body,0,body.Length); stream.Close(); } @R_403_6822@WebResponse webresp; try { webresp = (@R_403_6822@WebResponse)@R_403_6822@WebRequest.GetResponse(); } catch (WebException e) { //请求失败 throw new APIException((int)e.Status,e.Message); } if (webresp.StatusCode != @R_403_6822@StatusCode.OK) { throw new APIException((int)webresp.StatusCode,"Server response code:" + (int)webresp.StatusCode); } var respdata = Util.StreamToString(webresp.GetResponseStream(),ContentEnCoding); JObject respJobject; try { respJobject = JsonConvert.DeserializeObject<JObject>(respdata); } catch (Exception e) { throw new APIException(e.Message + ":" + respdata); } if (respJobject == null) { throw new APIException("Empty response!"); } return respJobject; } }
public static string StreamToString(Stream ss,EnCoding enc) { string ret; using (var reader = new StreamReader(ss,enc)) { ret = reader.ReadToEnd(); } ss.Close(); return ret; } public static string UrIEncode(string input,bool encodeSlash = false) { var builder = new StringBuilder(); foreach (var b in EnCoding.UTF8.GetBytes(input)) if (b >= ‘a‘ && b <= ‘z‘ || b >= ‘A‘ && b <= ‘Z‘ || b >= ‘0‘ && b <= ‘9‘ || b == ‘_‘ || b == ‘-‘ || b == ‘~‘ || b == ‘.‘) builder.Append((char)b); else if (b == ‘/‘) if (encodeSlash) builder.Append("%2F"); else builder.Append((char)b); else builder.Append(‘%‘).Append(b.ToString("X2")); return builder.ToString(); } public static string ParsequeryString(Dictionary<string,string> querys) { if (querys.Count == 0) return ""; return querys .Select(pair => pair.Key + "=" + pair.Value) .Aggregate((a,b) => a + "&" + b); }
try { var a = new New@R_403_6822@ClIEnt2("@R_403_6822@://localhost:100/.well-kNown/openID-configuration"); var b = a.PostAction(); var c = new New@R_403_6822@ClIEnt2(b["token_endpoint"].ToString()) { Bodys = new Dictionary<string,object> { { "scope","API1" },{ "grant_type","password" },{"clIEnt_ID","ro.clIEnt" },{"clIEnt_secret","secret" },{ "username","alice"},{"password","password" } },Method = "POST" }; var d = c.PostAction(); var e = new New@R_403_6822@ClIEnt2("@R_403_6822@://localhost:101/API/values/get") { BodyType=New@R_403_6822@ClIEnt2.BodyFormat.Json,headers=new Dictionary<string,string> { {"Authorization",d["token_type"].ToString() + " " + d["access_token"].ToString() } } }; var f = e.PostAction(); return Ok(f); } catch (APIException e) { return Ok(e); } catch(Exception e) { return Ok(e); }
[Serializable] public class APIException:Exception { public int Code { get; set; } public APIException() { Code = -1; } public APIException(string message) : base(message) { } public APIException(int code,string message) : base(message) { Code = code; } }
public static string StreamToString(Stream ss,b) => a + "&" + b); }
又琢磨@R_403_6822@新封装方法了,这个,估计更完善些,返回数据默认能转换为jobject,不然修改postaction下
总结以上是内存溢出为你收集整理的c# netcore http请求封装全部内容,希望文章能够帮你解决c# netcore http请求封装所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)