예제 #1
0
파일: KK.cs 프로젝트: yzhang226/RadarClient
        public static string ToText(object obj)
        {
            string data = "";

            if (obj is string)
            {
                data = (string)obj;
            }
            else if (obj is byte[])
            {
                data = Encoding.UTF8.GetString((byte[])obj);
            }
            else
            {
                data = Jsons.ToJson(obj);
            }

            return(data);
        }
예제 #2
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="filePaths"></param>
        /// <param name="statusCode"></param>
        /// <returns></returns>
        public static T PostWithFiles <T>(string url, object postData, List <string> filePaths, out int statusCode) where T : class, new()
        {
            // WebKitFormBoundaryafkSRjSyccnJC6ED
            string boundary = "------WebKitFormBoundary" + KK.CurrentMills();


            using (var httpContent = new MultipartFormDataContent(boundary))
            {
                // 1. form-data
                // Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryqgCWFrzZQTH8Ubnp

                if (postData != null)
                {
                    Dictionary <string, object> dict = Jsons.FromJson <Dictionary <string, object> >(Jsons.ToJson(postData));
                    foreach (var item in dict)
                    {
                        //Content-Disposition: form-data; name="json"
                        var stringContent = new StringContent(KK.ToText(item.Value));
                        stringContent.Headers.Add("Content-Disposition", "form-data; name=\"" + item.Key + "\"");
                        httpContent.Add(stringContent, item.Key);
                    }
                }

                // 2. file-data
                foreach (string path in filePaths)
                {
                    FileInfo fi    = new FileInfo(path);
                    string   fname = fi.Name;
                    int      idx   = fname.LastIndexOf(".");

                    ByteArrayContent bac = new ByteArrayContent(File.ReadAllBytes(path));
                    httpContent.Add(bac, idx > 0 ? fname.Substring(0, idx) : fname, fname);
                }

                // httpContent.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");// boundary=" + boundary
                // httpContent.Headers.ContentType.CharSet = "utf-8";

                string result = string.Empty;
                //异步Post
                try
                {
                    HttpResponseMessage response = uploadClient.PostAsync(url, httpContent).Result;
                    //输出Http响应状态码
                    statusCode = (int)response.StatusCode;
                    //确保Http响应成功
                    if (response.IsSuccessStatusCode)
                    {
                        //异步读取json
                        result = response.Content.ReadAsStringAsync().Result;
                    }

                    return(result?.Length > 0 ? Jsons.FromJson <T>(result) : null);
                }
                catch (Exception e)
                {
                    logger.Error("post url#" + url + " error.", e);
                    statusCode = -1;
                    return(default(T));
                }
            }
        }