예제 #1
0
 static HttpClinetHelper()
 {
     //建立 HttpClient http://localhost:5000/api/
     client             = new HttpClient();
     client.BaseAddress = new Uri(DNSHelper.getRemoteIPUrlPortPath(SettingConfig.PostUrl));
     // 指定 authorization header
     client.DefaultRequestHeaders.Add("Authorization", string.Format("{0}", SettingConfig.ApiTokenString));
 }
예제 #2
0
        /// <summary>
        /// 使用post方法异步请求
        /// </summary>
        /// <param name="url">目标链接</param>
        /// <param name="json">发送的参数字符串,只能用json</param>
        /// <returns>返回的字符串</returns>
        public static string PostJson(string url, string json)
        {
            var baseUrlStr = DNSHelper.getRemoteIPUrlPortPath(SettingConfig.PostUrl);

            if (restClient.BaseUrl != new Uri(baseUrlStr))
            {
                restClient.BaseUrl = new Uri(baseUrlStr);
            }

            var request = new RestRequest(url, Method.POST);

            request.Timeout = 1000 * 15;

            // easily add HTTP Headers
            request.AddHeader("Authorization", string.Format("{0}", SettingConfig.ApiTokenString));
            //request.AddHeader("User-Agent", string.Format("{0}", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"));

            request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
            request.AddParameter("cache-control", "no-cache", ParameterType.HttpHeader);
            request.RequestFormat = DataFormat.Json;

            //restClient.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Revalidate);
            restClient.MaxRedirects = 3;
            //restClient.Timeout = 1000 * 15;

            //// execute the request
            IRestResponse response = restClient.Execute(request);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var content = response.Content; // raw content as string
                return(content);
            }
            else
            {
                LogHelper.Error(response.StatusCode.ToString(), new Exception {
                    Source = response.StatusDescription
                });
                return("");
            }

            //return content;
            //// or automatically deserialize result
            //// return content type is sniffed but can be explicitly set via RestClient.AddHandler();
            //RestResponse<Person> response2 = client.Execute<Person>(request);
            //var name = response2.Data.Name;
        }
예제 #3
0
        /// <summary>
        /// 使用post方法请求
        /// </summary>
        /// <param name="url">目标链接</param>
        /// <param name="json">发送的参数字符串,只能用json</param>
        /// <returns>返回的字符串</returns>
        public static string PostJson(string url, Object data)
        {
            //url = "/Values/AccessEform";
            var baseUrlStr = DNSHelper.getRemoteIPUrlPortPath(SettingConfig.NewOAUrl);

            if (restClient.BaseUrl != new Uri(baseUrlStr))
            {
                restClient.BaseUrl = new Uri(baseUrlStr);
            }

            var request = new RestRequest(url, Method.POST);

            request.Timeout = 1000 * 10;

            // easily add HTTP Headers
            //request.AddHeader("User-Agent", string.Format("{0}", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"));

            request.AddParameter("application/json; charset=utf-8", data.SerializeModel(), ParameterType.RequestBody);
            request.AddParameter("cache-control", "no-cache", ParameterType.HttpHeader);
            request.RequestFormat = DataFormat.Json;

            //restClient.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Revalidate);
            restClient.MaxRedirects = 3;
            //restClient.Timeout = 1000 * 15;

            //// execute the request
            IRestResponse response = restClient.Execute(request);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var content = response.Content; // raw content as string
                return(content);
            }
            else
            {
                return("");
            }
        }
예제 #4
0
 static NewOARestSharpHttp()
 {
     restClient = new RestClient(new Uri(DNSHelper.getRemoteIPUrlPortPath(SettingConfig.NewOAUrl)));
 }