C# HttpClient

LenovoUser lenovoUser = null;
            string url = LenovoPrefix + "user/login_new";
            HttpClient httpClient = new HttpClient();
            List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();
            paramList.Add(new KeyValuePair<string, string>("loginType", loginType));
            paramList.Add(new KeyValuePair<string, string>("loginkey", loginkey));
            paramList.Add(new KeyValuePair<string, string>("password", password));
            try
            {
                var response = httpClient.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList)).Result;
                string result = response.Content.ReadAsStringAsync().Result;
                lenovoUser = JsonHelper.JsonToEntity<LenovoUser>(result);
            }
            catch (Exception ex)
            {
                PlatFormHelper.Mvc.PFPageHelper.LogException("GetLenovoSessionID", ex);
            }
            return lenovoUser;

Json

string start_dttm = _pSDeptService.GetMaxLastupddt().ToString("yyyy-MM-dd HH:mm:ss");
string end_dttm = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string url = PS_Url + "C_SERV_DEPT_POST.v1/";
string json = "{\"c_token_key\":\"" + c_token_key + "\",\"c_token_value\":\"" + c_token_value + "\",\"start_dttm\":\"" + start_dttm + "\",\"end_dttm\":\"" + end_dttm + "\"}";
HttpClient httpClient = new HttpClient();
HttpContent httpContent = new StringContent(json);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = httpClient.PostAsync(new Uri(url), httpContent).Result;
string result = response.Content.ReadAsStringAsync().Result;

Basic Auth

            string url = $"https://{LoginInfo.hostIp}/oauth2/token";
            if (url.StartsWith("https"))
            {
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            }
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{LoginInfo.userName}:{LoginInfo.password}")));
                HttpContent httpContent = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
                var response = httpClient.PostAsync(new Uri(url), httpContent).Result;
                string result = response.Content.ReadAsStringAsync().Result;
                var tokenResultModel = Newtonsoft.Json.JsonConvert.DeserializeObject<TokenResultModel>(result);
                this.tokenId = tokenResultModel.access_token;
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值