예제 #1
0
        public static ResponseObject Check(ParamApi param)
        {
            if (string.IsNullOrEmpty(param.url))
            {
                return(new ResponseObject(false, "url不能为空"));
            }
            if (string.IsNullOrEmpty(param.method))
            {
                param.method = "Get";
            }
            if (param.data == null)
            {
                param.data = new JObject();
            }
            if (string.IsNullOrWhiteSpace(param.contentType))
            {
                param.contentType = "application/json";
            }
            param.timeOut = param.timeOut == null ? 5 : param.timeOut;
            param.timeOut = param.timeOut <= 0 ? 5 : param.timeOut;

            return(new ResponseObject(true));
        }
예제 #2
0
        /// <summary>
        /// 包含文件WEBAPI请求
        /// </summary>
        /// <param name="ParamApi">接口参数</param>
        /// <returns></returns>
        public async static Task <ResponseObject> FileWebApi(ParamApi paramapi)
        {
            var isCheck = ParamApi.Check(paramapi);

            if (!isCheck.status)
            {
                return(isCheck);
            }

            HttpClient client = new HttpClient();

            client.Timeout = DateTime.Now.AddSeconds(paramapi.timeOut) - DateTime.Now;

            string resultStr = "";

            HttpResponseMessage response;
            var content = new MultipartFormDataContent();

            content.Headers.ContentType = new MediaTypeHeaderValue(paramapi.contentType);
            if (HttpContext.Current != null)
            {
                if (!string.IsNullOrWhiteSpace(HttpContext.Current.Request.FilePath))
                {
                    var files = HttpContext.Current.Request.Files;
                    foreach (HttpPostedFile item in files)
                    {
                        //添加文件参数,参数名为files,文件名为123.png
                        content.Add(new StreamContent(item.InputStream), "file", item.FileName);
                    }
                }
            }
            if (!string.IsNullOrWhiteSpace(paramapi.user) && !string.IsNullOrWhiteSpace(paramapi.password))
            {
                // 设置HTTP头Http Basic认证
                client.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Basic",
                                                  Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", paramapi.user, paramapi.password))));
            }
            try
            {
                switch (paramapi.method)
                {
                case "Get": response = await client.GetAsync(paramapi.url); break;

                case "Put": response = await client.PutAsync(paramapi.url, content); break;

                case "Delete": response = await client.DeleteAsync(paramapi.url); break;

                case "Post": response = await client.PostAsync(paramapi.url, content); break;

                case "Send": response = await client.SendAsync(new HttpRequestMessage()); break;

                case "Update": response = await client.GetAsync(paramapi.url); break;

                default: response = await client.GetAsync(paramapi.url); break;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                return(new ResponseObject(false, ex.Message));
            }
            if (response != null && response.IsSuccessStatusCode)
            {
                resultStr = await response.Content.ReadAsStringAsync();
            }
            if (response.IsSuccessStatusCode)
            {
                return(new ResponseObject(true, JsonConvert.DeserializeObject(resultStr)));
            }
            else
            {
                return(new ResponseObject(false, JsonConvert.DeserializeObject(resultStr)));
            }
        }