Example #1
0
        /// <summary>
        /// Put请求指定的URL地址
        /// </summary>
        /// <typeparam name="T">返回的json转换成指定实体对象</typeparam>
        /// <param name="url">URL地址</param>
        /// <param name="putData">提交到Web的Json格式的数据:如:{"ErrCode":"FileErr"}</param>
        /// <returns></returns>
        public static T PutAsJson <T>(string url, object putData) where T : class, new()
        {
            int    httpStatus;
            string json = PutAsJson(url, putData, out httpStatus);

            return(json?.Length > 0 ? Jsons.FromJson <T>(json) : null);
        }
Example #2
0
        /// <summary>
        /// Get请求指定的URL地址
        /// </summary>
        /// <typeparam name="T">返回的json转换成指定实体对象</typeparam>
        /// <param name="url">URL地址</param>
        /// <returns></returns>
        public static T GetAsJson <T>(string url) where T : class, new()
        {
            int    httpStatus;
            string json = GetAsJson(url, out httpStatus);

            return(json?.Length > 0 ? Jsons.FromJson <T>(json) : null);
        }
Example #3
0
        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);
        }
Example #4
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));
                }
            }
        }