Exemplo n.º 1
0
        /// <summary>
        /// 以body为载体上传文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="filePath"></param>
        /// <param name="paramList"></param>
        /// <param name="options"></param>
        /// <param name="clientSetting"></param>
        /// <returns></returns>
        public async static Task <string> PostFileAsync(string url, string filePath, IDictionary <string, string> paramList = null, HttpPostOptions options = null, Action <HttpClient> clientSetting = null)
        {
            options = options ?? new HttpPostOptions();

            var file = Path.GetFileName(filePath);

            using (var client = CreateHttpClient(options.Accept, options.AcceptCharset))
            {
                clientSetting?.Invoke(client);

                url = AppendParams(url, paramList);

                FileStream    stream        = File.OpenRead(filePath);
                StreamContent streamContent = new StreamContent(stream);
                streamContent.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
                streamContent.Headers.ContentDisposition.FileName = file;
                streamContent.Headers.ContentLength = stream.Length;
                streamContent.Headers.ContentType   = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(file));

                var response = await client.PostAsync(url, streamContent);

                response.EnsureSuccessStatusCode();

                return(options.Encoding.GetString(await response.Content.ReadAsByteArrayAsync()));

                #region
                //foreach (var path in filePaths)
                //{
                //    var fileContent = new ByteArrayContent(File.ReadAllBytes(path));
                //    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                //    {
                //        FileName = Path.GetFileName(path),
                //        Name = "files"
                //    };
                //    content.Add(fileContent);
                //}
                #endregion
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// PostForm请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="form"></param>
        /// <param name="paramList"></param>
        /// <param name="options"></param>
        /// <param name="clientSetting"></param>
        /// <returns></returns>
        public async static Task <string> PostFormAsync(string url, IDictionary <string, string> form, IDictionary <string, string> paramList = null, HttpPostOptions options = null, Action <HttpClient> clientSetting = null)
        {
            options = options ?? new HttpPostOptions();

            using (var client = CreateHttpClient(options.Accept, options.AcceptCharset))
            {
                clientSetting?.Invoke(client);

                url = AppendParams(url, paramList);

                var content = new FormUrlEncodedContent(form);

                var response = await client.PostAsync(url, content);

                response.EnsureSuccessStatusCode();

                return(options.Encoding.GetString(await response.Content.ReadAsByteArrayAsync()));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// PostMultipartForm请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="form"></param>
        /// <param name="filePaths"></param>
        /// <param name="fileFormName"></param>
        /// <param name="paramList"></param>
        /// <param name="options"></param>
        /// <param name="clientSetting"></param>
        /// <returns></returns>
        public async static Task <string> PostMultipartFormAsync(string url, IDictionary <string, string> form, IList <string> filePaths, string fileFormName, IDictionary <string, string> paramList = null, HttpPostOptions options = null, Action <HttpClient> clientSetting = null)
        {
            options = options ?? new HttpPostOptions();

            using (var client = CreateHttpClient(options.Accept, options.AcceptCharset))
            {
                clientSetting?.Invoke(client);

                url = AppendParams(url, paramList);

                using (var content = new MultipartFormDataContent())
                {
                    if (form != null)
                    {
                        foreach (var key in form.Keys)
                        {
                            content.Add(new StringContent(form[key]), key);
                        }
                    }

                    if (filePaths != null)
                    {
                        foreach (var path in filePaths)
                        {
                            var file = Path.GetFileName(path);

                            FileStream    stream        = File.OpenRead(path);
                            StreamContent streamContent = new StreamContent(stream);
                            streamContent.Headers.ContentDisposition          = new ContentDispositionHeaderValue("form-data");
                            streamContent.Headers.ContentDisposition.Name     = fileFormName ?? "files";
                            streamContent.Headers.ContentDisposition.FileName = file;
                            streamContent.Headers.ContentLength = stream.Length;
                            streamContent.Headers.ContentType   = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(file));
                            content.Add(streamContent);
                        }
                    }

                    var response = await client.PostAsync(url, content);

                    response.EnsureSuccessStatusCode();

                    return(options.Encoding.GetString(await response.Content.ReadAsByteArrayAsync()));
                }
            }
        }