예제 #1
0
        public async Task <ApiResponseStr> ActionAsync(HttpVerb verb, string path, object body = null)
        {
            HttpResponseMessage res     = null;
            HttpContent         content = null;

            if (body != null)
            {
                if (body is string)
                {
                    content = new StringContent(body as string, Encoding.UTF8, "application/json");
                }
                else
                {
                    content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
                }
            }
            string resBody = "";

            try
            {
                switch (verb)
                {
                case HttpVerb.Get: res = await client.GetAsync(path); break;

                case HttpVerb.Post: res = await client.PostAsync(path, content); break;

                case HttpVerb.Put: res = await client.PutAsync(path, content); break;

                case HttpVerb.Delete: res = await client.DeleteAsync(path); break;
                }
                resBody = await res.Content.ReadAsStringAsync();
            }catch (Exception ex)
            {
                resBody = ex.Message;
            }
            ApiResponseStr result = new ApiResponseStr();

            result.StatusCode = res == null ? 0 : (int)res.StatusCode;
            result.IsSuccess  = res == null ? false : res.IsSuccessStatusCode;
            result.Content    = resBody;
            return(result);
        }
예제 #2
0
        public async Task <ApiResponseStr> UploadFile(string url, string localPath, int fileState = 0)
        {
            string urlEncodedPath      = System.Net.WebUtility.UrlEncode(localPath);// for http header safe
            string fileExt             = System.IO.Path.GetExtension(urlEncodedPath);
            HttpResponseMessage res    = null;
            ApiResponseStr      result = new ApiResponseStr();

            HttpClient c = new HttpClient();

            c.BaseAddress = serverBase;
            c.DefaultRequestHeaders.Add("Authorization", "bearer " + token);
            c.DefaultRequestHeaders.Add("fileExt", fileExt);
            c.DefaultRequestHeaders.Add("localPath", urlEncodedPath);
            c.DefaultRequestHeaders.Add("fileState", fileState.ToString());

            HttpContent content = null;

            try
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(localPath, System.IO.FileMode.Open))
                {
                    content = new StreamContent(fs);
                    res     = await c.PostAsync(url, content);
                }
            }catch (Exception ex)
            {
                result.Content    = ex.Message;
                result.IsSuccess  = false;
                result.StatusCode = 400;
                return(result);
            }
            string resBody = await res.Content.ReadAsStringAsync();

            result.StatusCode = (int)res.StatusCode;
            result.IsSuccess  = res.IsSuccessStatusCode;
            result.Content    = resBody;
            return(result);
        }