HttpClient的post提交

  • binGe博客
  • 开发笔记
  • 2024/1/25 17:00:42
  • 人已阅读
简介
        /// <summary>
        /// 普通的post提交
        /// </summary>
        /// <param name="_url"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        public static string HttpPost(string _url, string postData)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                HttpContent httpContent = new StringContent(postData);
                httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                HttpResponseMessage response = httpClient.PostAsync(_url, httpContent).Result;
                if (response.IsSuccessStatusCode)
                {
                    var data = response.Content.ReadAsStringAsync().Result;
                    return data;
                }
                return "";
            }
        }
        /// <summary>
        /// 墨斗文件上传
        /// </summary>
        /// <param name="_url"></param>
        /// <param name="appId"></param>
        /// <param name="token"></param>
        /// <param name="fileParams"></param>
        /// <returns></returns>
        public static string HttpPostForm(string _url, string appId, string token, ModoFileItem fileParams)
        {
            try
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x"));
                    MultipartFormDataContent postContent = new MultipartFormDataContent();
                    postContent.Headers.Add("ContentType", $"multipart/form-data;charset=utf-8;boundary={boundary}");
                    postContent.Headers.Add("appId", appId);
                    postContent.Headers.Add("token", token);
                    //文件参数
                    HttpContent content = new StreamContent(fileParams.file, (int)fileParams.file.Length);
                    ContentDispositionHeaderValue contentDisposition = new ContentDispositionHeaderValue("form-data");
                    contentDisposition.Name = "file";
                    contentDisposition.FileName = fileParams.fileMd5;
                    content.Headers.ContentDisposition = contentDisposition;
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    postContent.Add(content);
                    HttpResponseMessage response = httpClient.PostAsync(_url, postContent).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        var data = response.Content.ReadAsStringAsync().Result;
                        return data;
                    }
                    return "";
                }
            }
            catch (Exception ex)
            {
                return "";
            }
            finally
            {
                fileParams.file.Close();
            }
        }

文章评论

评论
  • 消灭零回复
Top