public AccessTokenResponse GetTokenFromServer() { string nonce = Guid.NewGuid().ToString("N"); //随机数 string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //时间戳 IDictionary <string, string> headerParames = new Dictionary <string, string>(); //参数摘要计算得map headerParames.Add("X-FDD-Api-App-Id", AppId); headerParames.Add("X-FDD-Api-Sign-Type", "HMAC-SHA256"); headerParames.Add("X-FDD-Api-Timestamp", timestamp); headerParames.Add("X-FDD-Api-Nonce", nonce); headerParames.Add("X-FDD-Api-Grant-Type", "client_credential"); headerParames.Add("X-FDD-Api-Sign", Utility.SignUtil.GetSign(headerParames, timestamp, AppKey)); var client = HttpWebHelp.CreateDefault(ServerUrl + "/oauth2/accessToken"); foreach (var p in headerParames) { client.Headers[p.Key] = p.Value; } client.Method = "POST"; client.ContentType = "application/x-www-form-data"; var rspResponse = client.GetResponseIgnoreServerError(); var rspStr = rspResponse.GetResponseString(); var rspToken = JsonConvert.DeserializeObject <BaseResponse <AccessTokenResponse> >(rspStr); rspToken.RequestId = rspResponse.Headers["X-FDD-Request-Id"]; return(rspToken.data); }
public BaseResponse <T> Execute <T>(BaseReqeust <T> req) where T : class, new() { var reqStr = JsonConvert.SerializeObject(req, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); var urlAttribute = GetUrlAttribute(req); var url = ServerUrl + urlAttribute.Url; BaseResponse <T> rspModel = null; try { var body = new Dictionary <string, string>() { { "bizContent", reqStr } }; var files = GetRequestFiles(req); var client = HttpWebHelp.CreateDefault(url); var token = GetToken(); SetRequestHeaders(client, token.accessToken, body); client.Method = urlAttribute.Method; client.ContentType = "multipart/form-data"; client.SubmitFormData(body, files); var rsp = client.GetResponseIgnoreServerError(); string rspStr = null; if (typeof(FileItem).IsAssignableFrom(typeof(T))) { if (rsp.ContentType.StartsWith("application/json")) { rspStr = rsp.GetResponseString(); rspModel = JsonConvert.DeserializeObject <BaseResponse <T> >(rspStr); rspModel.RequestId = rsp.Headers["X-FDD-Request-Id"]; } else { var file = new T() as FileItem; file.Stream = new MemoryStream(); //此处可根据request配置,直接传递filestream,直接就下载到本地存储,可以不用先下载到内存。 rsp.GetResponseFileItem(file); rspModel = new BaseResponse <T>() { code = 100000, msg = "文件下载成功!", data = file as T }; } } else { rspStr = rsp.GetResponseString(); rspModel = JsonConvert.DeserializeObject <BaseResponse <T> >(rspStr); rspModel.RequestId = rsp.Headers["X-FDD-Request-Id"]; } } catch (Exception e) { rspModel = new BaseResponse <T>() { code = -1, msg = "Client:" + e.Message }; } return(rspModel); }