public WsModel <Trequest, Tresponse> Invoke <Trequest, Tresponse>(string requestUri, WsModel <Trequest, Tresponse> model) { var api = QuickWebApiFactory.Instance.Get(_service); if (api == null) { model.ERROR(-9999998, string.Format("未找到{0}->{1}的webapi配置", _service, requestUri)); return(model); } if (string.IsNullOrWhiteSpace(api.Uri)) { model.ERROR(-9999997, "未指定服务地址"); return(model); } if (string.IsNullOrWhiteSpace(api.Url(requestUri))) { model.ERROR(-9999996, "未指定接口路由"); return(model); } using (WebApiClient client = new WebApiClient(api.Uri)) { var mtd = api.Method(requestUri); return(client.Invoke <Trequest, Tresponse>(api.Url(requestUri), model, mtd.Method)); } }
protected WsModel <string, Tresponse> _invoke <T, Tresponse>(Expression exp, params object[] args) { var method = ((exp as UnaryExpression).Operand as MethodCallExpression); string code = ((method.Object as ConstantExpression).Value as MethodInfo).Name; foreach (var m in method.Arguments) { if (m.Type == typeof(T)) { var attr = m.Type.GetCustomAttribute <WebApiAttribute>(); StringBuilder sb = new StringBuilder(); var pis = m.Type.GetMethod(code).GetParameters(); for (int i = 0; i < pis.Length; i++) { sb.AppendFormat("{0}={1}&", pis[i].Name, args[i] is DateTime ? ((DateTime)args[i]).ToString("yyyy-MM-dd HH:mm:ss") : args[i]); } if (attr != null) { return(new Invoker(build_server(attr.service)).Invoke <Tresponse>(code, sb.ToString())); } } } var model = new WsModel <string, Tresponse>(); model.ERROR(-9999999, "未能找到合适的api定义"); return(model); }
public WsModel <string, Tresponse> Invoke <Tresponse>(string requestUri, string data, MethodType mtd) { WsModel <string, Tresponse> model = null; using (var client = new HttpClient()) { Append_Header(client); HttpResponseMessage ret = null; client.BaseAddress = _uri; BuildHeader(client.DefaultRequestHeaders, client.BaseAddress.Authority, requestUri, "POST"); if (mtd == MethodType.HTTPGET) { ret = client.GetAsync(string.Format("{0}?{1}", requestUri, data)).Result; } else if (mtd == MethodType.HTTPPOST) { ret = client.PostAsync(string.Format("{0}?{1}", requestUri, data), new StringContent(string.Empty)).Result; } else if (mtd == MethodType.HTTPDEL) { ret = client.DeleteAsync(string.Format("{0}?{1}", requestUri, data)).Result; } else { model = new WsModel <string, Tresponse>(); model.ERROR(-9999990, string.Format("{0}/{1}未配置{2}请求", _uri.AbsoluteUri, requestUri, mtd.ToString())); return(model); } model = HttpResponseMessage2WSModel <string, Tresponse>(ret); model.Request = data;// string.Format("{0}:{1}/{2}", mtd.ToString(), _uri.AbsoluteUri, requestUri); return(model); } }
public WsModel HttpResponseMessage2WSModel(HttpResponseMessage response) { if (response == null) { WsModel model = new WsModel(); model.ERROR(-9999990, "Null HttpResponseMessage"); return(model); } if (!response.IsSuccessStatusCode) { WsModel model = new WsModel(); model.ERROR((int)response.StatusCode, response.ReasonPhrase); return(model); } return(response.Content.ReadAsAsync <WsModel>().Result); }