/// <summary> /// 具体处理 /// </summary> /// <returns></returns> public override T GetResponse() { string getJosn = string.Empty, rjson = string.Empty; WebUtils webutils = new WebUtils(); //判断是否需要身份验证 if (base.HttpMethodAttribute.IsToken) { base.HttpMethodAttribute.Url = base.HttpMethodAttribute.Url + WeiXinUtils.BuildGetUrl(base.HttpMethodAttribute.Url ) + "access_token=" + base.Token.AccessToken; } //如果手动拼写请求参数 if (base.HttpMethodAttribute.Serialize == SerializeVerb.None) { //url参数组合 Type type = base.Request.GetType(); PropertyInfo[] finfos = type.GetProperties(); StringBuilder sb = new StringBuilder(); foreach (PropertyInfo finfo in finfos) { string fieldName = finfo.Name; string fieldValue = string.Empty; object objValue = finfo.FastGetValue(Request); FieldInfo fieldInfo = type.GetField(fieldName); //获得是否为存在get参数 GetParameterAttribute getData = (GetParameterAttribute)System.Attribute.GetCustomAttribute(finfo, typeof(GetParameterAttribute)); if (getData != null && (objValue is Int32 || objValue is string)) { fieldValue = objValue.ToString(); //是否是必须参数 if (getData.IsRequired) { if (string.IsNullOrEmpty(fieldValue)) { throw new WeiXinException(string.Format("{0}属性值 不能为空", fieldName)); } else { sb.Append(getData.Name ?? fieldName); sb.Append("="); sb.Append(fieldValue); sb.Append("&"); } } //如果为不必填参数 else { //如果得到此值,则拼接上去 if (!string.IsNullOrEmpty(fieldValue)) { sb.Append(fieldName); sb.Append("="); sb.Append(fieldValue); sb.Append("&"); } } } //获得是否为存在get参数 PostParameterAttribute postData = (PostParameterAttribute)System.Attribute.GetCustomAttribute(finfo, typeof(PostParameterAttribute)); if (postData != null) { if (postData.Serialize == SerializeVerb.Json) { rjson = objValue.objToJson(); } } } if (sb.Length > 0) { if (sb.ToString().EndsWith("&")) { sb.Remove(sb.Length - 1, 1); } base.HttpMethodAttribute.Url = base.HttpMethodAttribute.Url + WeiXinUtils.BuildGetUrl(base.HttpMethodAttribute.Url ) + sb.ToString(); } } //创建菜单单独处理 if (base.HttpMethodAttribute.Serialize == Attribute.SerializeVerb.Json) { rjson = Request.objToJson(); } getJosn = webutils.DoPost(base.HttpMethodAttribute.Url, rjson); return(getJosn.jsonToObj <T>()); }
public override T GetResponse() { //判断是否需要身份验证 if (base.HttpMethodAttribute.IsToken) { base.HttpMethodAttribute.Url = base.HttpMethodAttribute.Url + WeiXinUtils.BuildGetUrl(base.HttpMethodAttribute.Url ) + "access_token=" + base.Token.AccessToken; } WebUtils webutils = new WebUtils(); //上传文件返回json if (HttpMethodAttribute.Serialize == SerializeVerb.Json) { Type type = base.Request.GetType(); //获得多媒体路径. PropertyInfo infoMedia = type.GetProperty("Media"); object objMedia = infoMedia.FastGetValue(base.Request); // object objMedia = infoMedia.GetValue(Request, null); string strPath = string.Empty; if (objMedia is string) { strPath = objMedia as string; } //获得多媒体类型. PropertyInfo infoType = type.GetProperty("Type"); object objType = infoType.FastGetValue(base.Request); Domain.MediaType mediaType = new Domain.MediaType(); if (objType is Domain.MediaType) { mediaType = (Domain.MediaType)objType; } ///如果路径不为空 if (File.Exists(strPath)) { //在url里面添加文件类型 HttpMethodAttribute.Url += "&type=" + mediaType.ToString().ToLower(); string strJson = webutils.DoPostFile(HttpMethodAttribute.Url, strPath); return(strJson.jsonToObj <T>()); } else { throw new WeiXinException(string.Format("你要上传的:{0}路径非法", strPath)); } } ///返回byte 执行下载逻辑 if (HttpMethodAttribute.Serialize == SerializeVerb.Byte) { Type type = base.Request.GetType(); PropertyInfo[] finfos = type.GetProperties(); StringBuilder sb = new StringBuilder(); foreach (PropertyInfo finfo in finfos) { string fieldName = finfo.Name; string fieldValue = string.Empty; object objValue = finfo.FastGetValue(Request); if (objValue is Int32 || objValue is string) { fieldValue = objValue.ToString(); } FieldInfo fieldInfo = type.GetField(fieldName); DataMemberAttribute data = (DataMemberAttribute)System.Attribute.GetCustomAttribute(finfo, typeof(DataMemberAttribute)); if (data != null) { //是否是必须参数 if (data.IsRequired) { if (string.IsNullOrEmpty(fieldValue)) { throw new WeiXinException(string.Format("{0}属性值 不能为空", fieldName)); } else { sb.Append(data.Name ?? fieldName); sb.Append("="); sb.Append(fieldValue); sb.Append("&"); } } else { if (!string.IsNullOrEmpty(fieldValue)) { sb.Append(fieldName); sb.Append("="); sb.Append(fieldValue); sb.Append("&"); } } } } if (sb.ToString().EndsWith("&")) { sb.Remove(sb.Length - 1, 1); } ///得到请求Url base.HttpMethodAttribute.Url = base.HttpMethodAttribute.Url + WeiXinUtils.BuildGetUrl(base.HttpMethodAttribute.Url ) + sb.ToString(); string path = string.Empty; byte[] stream = webutils.Download(HttpMethodAttribute.Url, out path); GetMediaResponse response = new GetMediaResponse(); response.Stream = BytesToStream(stream); response.Path = path; return(response as T); } return(null); }
/// <summary> /// 具体业务实现。 /// </summary> /// <returns></returns> public override T GetResponse() { //判断是否需要身份验证 if (base.HttpMethodAttribute.IsToken) { base.HttpMethodAttribute.Url = base.HttpMethodAttribute.Url + WeiXinUtils.BuildGetUrl(base.HttpMethodAttribute.Url ) + "access_token=" + base.Token.AccessToken; } Type type = base.Request.GetType(); PropertyInfo[] finfos = type.GetProperties(); StringBuilder sb = new StringBuilder(); foreach (PropertyInfo finfo in finfos) { object val = finfo.FastGetValue(Request); string fieldName = finfo.Name; string fieldValue = string.Empty; object objValue = finfo.FastGetValue(Request); if (objValue is Int32 || objValue is string) { fieldValue = finfo.FastGetValue(Request).ToString(); } DataMemberAttribute data = (DataMemberAttribute)System.Attribute.GetCustomAttribute(finfo, typeof(DataMemberAttribute)); if (data != null) { //是否是必须参数 if (data.IsRequired) { if (string.IsNullOrEmpty(fieldValue)) { throw new WeiXinException(string.Format("{0}属性值 不能为空", fieldName)); } else { sb.Append(data.Name ?? fieldName); sb.Append("="); sb.Append(fieldValue); sb.Append("&"); } } else { if (!string.IsNullOrEmpty(fieldValue)) { sb.Append(fieldName); sb.Append("="); sb.Append(fieldValue); sb.Append("&"); } } } } if (sb.Length > 0) { if (sb.ToString().EndsWith("&")) { sb.Remove(sb.Length - 1, 1); } base.HttpMethodAttribute.Url = base.HttpMethodAttribute.Url + WeiXinUtils.BuildGetUrl(base.HttpMethodAttribute.Url ) + sb.ToString(); } WebUtils webutils = new WebUtils(); string strJosn = webutils.DoGet(base.HttpMethodAttribute.Url); return(strJosn.jsonToObj <T>()); }