<span id="hw99t"></span>
  • <dd id="hw99t"></dd>

    <dd id="hw99t"><noscript id="hw99t"></noscript></dd>
    <button id="hw99t"></button>
    <button id="hw99t"></button>

  • <em id="hw99t"></em>
    <dd id="hw99t"><track id="hw99t"></track></dd>

    <button id="hw99t"><object id="hw99t"></object></button>
    <em id="hw99t"></em>

    <tbody id="hw99t"></tbody>

      1. C#-Winform-.NetCore-WebApi-Post-JSON數據

        用HttpClient訪問Asp.Net Core WebAPI



        數據庫學習 【2021-08-20 09:53】


        HttpClient訪問Asp.Net Core WebAPI


        后端代碼:

         [AllowAnonymous]
         [HttpPost]
         public ActionResult login([FromBody] User request)
         {
           if (1==1)
           {
             return Ok(new                
             {
               Token = token,
               Message = "Success"
             });
           } 
           return BadRequest("Invalid Request"); 
        }   
        
        [Authorize]
        public string GetItemInfo()
        {
          var item = (from s in _customercontext.Item
                           select s).ToList();
          return JsonConvert.SerializeObject(item);
        }

        前端代碼:

        private void button1_Click(object sender, EventArgs e)
        {
        
            string url = "http://8.8.8.8:8888/api/login";
         
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("username", txtLoginId.Text);
            dic.Add("password", txtPassword.Text);
         
            string req_str = JsonConvert.SerializeObject(dic);//序列化成JSON
            string result = Post(url, req_str);
        
            ret = JsonConvert.DeserializeObject<Dictionary<string, string>>(result);//反序列化
             
            txtToken.Text = ret["token"].ToString();
         
        } 
        //Post with JSON
        static  Post(string url, string req_str)
        {
            HttpClient client = new HttpClient();
            var content = new StringContent(req_str);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
         
            var response = client.PostAsync(url, content);
            response.Wait();
            response.Result.EnsureSuccessStatusCode();
            var res = response.Result.Content.ReadAsStringAsync();
            res.Wait();
            return res.Result;
        }
        
        //Get with tokent:
        static string Get(string url, string token)
        {
            HttpClient client = new HttpClient();
         
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
         
            var response = client.GetAsync(url);
            response.Wait();
            response.Result.EnsureSuccessStatusCode();
            var res = response.Result.Content.ReadAsStringAsync();
            res.Wait();
            return res.Result;
        }


        友情鏈接:
        SQL數據庫修復專家
        • 關注微博
          聯系QQ
        • 二維碼
        • 掃一掃加關注

        ——信陽市速全網絡科技有限公司——

        版權所有:信陽市速全網絡科技有限公司

        豫ICP備19030104號-2

        女人夜色黄网在线观看,高清国语自产拍在线,亚洲色国产欧美日韩,熟女精品视频一区
        <span id="hw99t"></span>
      2. <dd id="hw99t"></dd>

        <dd id="hw99t"><noscript id="hw99t"></noscript></dd>
        <button id="hw99t"></button>
        <button id="hw99t"></button>

      3. <em id="hw99t"></em>
        <dd id="hw99t"><track id="hw99t"></track></dd>

        <button id="hw99t"><object id="hw99t"></object></button>
        <em id="hw99t"></em>

        <tbody id="hw99t"></tbody>