• c# 后台post,包含file文件


    http request 相关

            private bool Upload(KeyValuePair<string, string>[] paramString,Stream paramFileStream)
            {
                var actionUrl = $"{_websiteUrl}/********";
    
                byte[] paramFileBytes = new byte[paramFileStream.Length];
                paramFileStream.Read(paramFileBytes, 0, paramFileBytes.Length);
                // 设置当前流的位置为流的开始 
                paramFileStream.Seek(0, SeekOrigin.Begin);
                Func<Task<bool>> task = async () =>
                {
                    using (var client = CreateHttpClient())
                    {
                        using (var formData = new MultipartFormDataContent())
                        {
    
                            foreach (var keyValuePair in paramString)
                            {
                                formData.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
                            }
                            //var fileContent = new ByteArrayContent(paramFileBytes);
                            HttpContent fileStreamContent = new StreamContent(paramFileStream);
                            fileStreamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
                            {
                                Name = "files",
                                Size = 1,
                                FileName = "201809.xlsx"
                            };
                            fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                            //formData.Add(fileContent, "files");
                            formData.Add( fileStreamContent, "files", "201809.xlsx");
                            client.DefaultRequestHeaders.Add("UserToken", _clientAuth.GetToken());
                            var respMessage = await client.PostAsync(FixUrl(actionUrl), formData).ConfigureAwait(false);
                            if (!respMessage.IsSuccessStatusCode)
                            {
                                return false;
                            }
                            //respMessage.EnsureSuccessStatusCode();
                            Byte[] rawBytes = await respMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
                            var content = ClientEncoding.GetString(rawBytes, 0, rawBytes.Length);
                            return true;
                        }
                    }
                };
                return RunSync(task);
            }    
    

    相关方法

            public static TResult RunSync<TResult>(Func<Task<TResult>> func, bool await = false)
            {
                CultureInfo cultureUi = CultureInfo.CurrentUICulture;
                CultureInfo culture = CultureInfo.CurrentCulture;
                return _myTaskFactory.StartNew<Task<TResult>>(delegate
                {
                    Thread.CurrentThread.CurrentCulture = culture;
                    Thread.CurrentThread.CurrentUICulture = cultureUi;
                    return func();
                }).Unwrap<TResult>().ConfigureAwait(await).GetAwaiter().GetResult();
            }
    

      

  • 相关阅读:
    Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies
    Educational Codeforces Round 55 (Rated for Div. 2)
    Educational Codeforces Round 55 (Rated for Div. 2) A
    洛谷 P1061 Jam的计数法
    Codeforces Round #523 (Div. 2) B Views Matter
    Codeforces Round #523 (Div. 2) C Multiplicity (DP)
    2018.11.26 QLU新生赛部分题解
    动态规划:分组背包和有依赖的背包
    动态规划:二维费用背包
    动态规划:混合背包
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/10001934.html
Copyright © 2020-2023  润新知