备份一个http/https请求,用的比较多的POST json数据

var data = new object[] { new { sn = SN, mac = Mac } };

var jobj = await Task.Run(() =>
            {
                try
                {
using (HttpClient client = new HttpClient())
                    {
                        var url = Helper.GetAppSettings("SubmitAddress");
                        var json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
                        client.Timeout = TimeSpan.FromSeconds(5);
                        var result = client.PostAsync(url, new StringContent(json, Encoding.UTF8, "application/json")).Result;
                        var ack = result.Content.ReadAsStringAsync().Result;

               result.Content.Dispose();
               result.Dispose();

                        return JObject.Parse(ack);

                    }
                }
                catch (Exception ex)
                {
                    return JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(new
                    {
                        code = 500,
                        msg = $"服务器错误,请核查数据提交地址,配置文件中“SubmitAddress”是否正确!\r\n{ex.Message}"
                    }));
                }
            });
            loadCtrl.IsOpen = false;

            var code = jobj["code"]?.Value<int>();
            var msg = jobj["msg"]?.Value<string>();

 

带证书的情况

// https 忽略证书
WebRequestHandler handler = new WebRequestHandler();
handler.ServerCertificateValidationCallback = delegate { return true; };
HttpClient client = new HttpClient(handler);

string msg = string.Empty, webToken = string.Empty;
using (HttpClient client = new HttpClient(handler))
{
    try
    {
        var url = "https://10.6.30.156/api/common/v1/user/login?username=nms&password=nms@a12345";
        client.Timeout = TimeSpan.FromSeconds(10);
        //MultipartFormDataContent content = new MultipartFormDataContent();
        //content.Add(new StringContent("admin"), "username");
        //content.Add(new StringContent("Dccs12345."), "password");
        var result = client.PostAsync(url, null).Result;
        if (result.StatusCode == System.Net.HttpStatusCode.OK)
        {
            try
            {
                var ack = result.Content.ReadAsStringAsync().Result;
                result.Content.Dispose();
                result.Dispose();

                var jobj = JObject.Parse(ack);
                if (jobj.Value<bool>("success"))
                {
                    var jresult = jobj.Value<JObject>("result");
                    webToken = jresult.Value<string>("webToken");
                }
                else
                {
                    msg = $"{jobj.Value<string>("msg")}";
                }
            }
            catch (Exception ex)
            {
                msg = $"数据解析异常,{ex.Message}";
            }
        }
        else
        {
            msg = $"远程服务器状态响应不正确,{result.StatusCode}";
        }

    }
    catch (Exception ex)
    {
        msg = $"访问异常,{ex.Message}";
    }
}

 

posted @ 2021-09-29 18:08  吃奶嘴的路飞  阅读(44)  评论(0编辑  收藏  举报