Exemplo n.º 1
0
        public static T GetLocal <T>(string url, out string message)
        {
            try
            {
                using (HttpClient client = new HttpClient(new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip
                                             | DecompressionMethods.Deflate
                }))
                {
                    HttpResponseMessage response = null;
                    client.DefaultRequestHeaders.Authorization = CreateBasicCredentials(ServerProfile.GetInstance().REMOTE_USER_NAME, ServerProfile.GetInstance().REMOTE_USER_PASSWORD);
                    client.BaseAddress = new Uri(ServerProfile.GetInstance().LOCAL_API_SITE);

                    client.Timeout = new System.TimeSpan(2, 0, 0);

                    response = client.Get(ServerProfile.GetInstance().LOCAL_API_SITE + url);

                    if (response == null)
                    {
                        message = string.Format("執行結果無回傳值: {0}{1}", ServerProfile.GetInstance().REMOTE_API_SITE, url);
                        return(default(T));
                    }
                    else if (response.IsSuccessStatusCode == false)
                    {
                        message = string.Format("回傳錯誤 :{0:D} {1}", response.StatusCode, response.ReasonPhrase);
                        return(default(T));
                    }

                    message = string.Empty;

                    return(response.Content.ReadAsAsync <T>().Result);
                }
            }
            catch (AggregateException ex)
            {
                if (ex.InnerException != null)
                {
                    if (ex.InnerException.InnerException != null)
                    {
                        if (ex.InnerException.InnerException is System.Net.WebException)
                        {
                            throw ex.InnerException.InnerException;
                        }
                        else
                        {
                            throw ex.InnerException;
                        }
                    }
                    else
                    {
                        throw ex.InnerException;
                    }
                }
                else
                {
                    throw ex;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 取得網頁上的字串資訊
        /// </summary>
        /// <param name="url"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string GetContentStr(string url, out string message)
        {
            try
            {
                using (HttpClient client = new HttpClient(new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip
                                             | DecompressionMethods.Deflate
                }))
                {
                    HttpResponseMessage response = null;

                    client.Timeout = new System.TimeSpan(2, 0, 0);

                    response = client.Get(url);

                    if (response == null)
                    {
                        message = string.Format("執行結果無回傳值: {0}{1}", ServerProfile.GetInstance().REMOTE_API_SITE, url);
                        return(default(string));
                    }
                    else if (response.IsSuccessStatusCode == false)
                    {
                        message = string.Format("回傳錯誤 :{0:D} {1}", response.StatusCode, response.ReasonPhrase);
                        return(default(string));
                    }

                    message = "ok";

                    return(response.Content.ReadAsStringAsync().Result);
                }
            }
            catch (AggregateException ex)
            {
                if (ex.InnerException != null)
                {
                    if (ex.InnerException.InnerException != null)
                    {
                        if (ex.InnerException.InnerException is System.Net.WebException)
                        {
                            throw ex.InnerException.InnerException;
                        }
                        else
                        {
                            throw ex.InnerException;
                        }
                    }
                    else
                    {
                        throw ex.InnerException;
                    }
                }
                else
                {
                    throw ex;
                }
            }
        }
Exemplo n.º 3
0
        public static ServerProfile GetInstance()
        {
            if (_serverProfile == null)
            {
                _serverProfile = new ServerProfile();
            }


            return(_serverProfile);
        }
Exemplo n.º 4
0
        private static T Put <T, K>(string host, string path, K obj, out string message)
        {
            string address = host + path;

            try
            {
                HttpResponseMessage response = null;

                using (HttpClient client = new HttpClient(new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip
                                             | DecompressionMethods.Deflate
                }))
                {
                    client.DefaultRequestHeaders.Authorization =
                        CreateBasicCredentials(
                            ServerProfile.GetInstance().REMOTE_USER_NAME,
                            ServerProfile.GetInstance().REMOTE_USER_PASSWORD);
                    client.Timeout     = new System.TimeSpan(2, 0, 0);
                    client.BaseAddress = new Uri(host);
                    // Add an Accept header for JSON format.
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

                    response = client.PutAsJsonAsync <K>(address, obj).Result;

                    if (response == null)
                    {
                        message = string.Format("執行結果無回傳值: {0}", address);
                        return(default(T));
                    }
                    else if (response.IsSuccessStatusCode == false)
                    {
                        message = string.Format("回傳錯誤 :{0:D} {1}",
                                                response.StatusCode, response.ReasonPhrase);
                        return(default(T));
                    }
                    else
                    {
                        message = string.Empty;
                        return(response.Content.ReadAsAsync <T>().Result);
                    }
                }
            }
            catch (AggregateException ex)
            {
                if (ex.InnerException != null)
                {
                    if (ex.InnerException.InnerException != null)
                    {
                        if (ex.InnerException.InnerException is System.Net.WebException)
                        {
                            throw ex.InnerException.InnerException;
                        }
                        else
                        {
                            throw ex.InnerException;
                        }
                    }
                    else
                    {
                        throw ex.InnerException;
                    }
                }
                else
                {
                    throw ex;
                }
            }
        }
Exemplo n.º 5
0
 public static T PutLocal <T, K>(string path, K obj, out string message)
 {
     return(Put <T, K>(ServerProfile.GetInstance().LOCAL_API_SITE, path, obj, out message));
 }
Exemplo n.º 6
0
 /// <summary>
 /// HttpPOST + BasicCredentials
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <typeparam name="K"></typeparam>
 /// <param name="url"></param>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static T PutRemote <T, K>(string path, K obj, out string message)
 {
     return(Put <T, K>(ServerProfile.GetInstance().REMOTE_API_SITE, path, obj, out message));
 }