• winform httplicent调用API


    绑定datagriview

       Uri uri = new Uri("http://localhost:49423");//地址
                HttpClient clien = new HttpClient();
                clien.BaseAddress = uri;
                clien.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                HttpResponseMessage message = null;
                if(string.IsNullOrWhiteSpace(name))
                message= clien.GetAsync("api/show/Get").Result;
                else
                    message = clien.GetAsync("api/show/Getcha?id="+name).Result;
                if (message.IsSuccessStatusCode)
                {
                    var data = message.Content.ReadAsStringAsync().Result;

                    var data2 = JsonConvert.DeserializeObject<DataTable>(data);


                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.AllowUserToAddRows = false;
                    dataGridView1.DataSource = data2;

    修改

       /// <summary>
            /// 修改
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {

                update up = new update();
                up.names.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                 up.id.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                up.zhiwu.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                up.bili.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
                up.phone.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
                up.dizhi.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
                up.beizhu.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();

                up.ShowDialog();//showdialog修改之后还能执行下面的代码
                bind("");
            }

    保存修改

       private void button1_Click(object sender, EventArgs e)
            {
                yuan mm = new yuan()
                {
                    Beizhu = beizhu.Text,
                    Bili = Convert.ToDecimal(bili.Text),
                    Dizhi = dizhi.Text,
                    Names = names.Text,
                    Phone = phone.Text,
                    Zhiwu = zhiwu.Text,
                    id= Convert.ToInt32( id.Text)
                
                };
                Uri uri = new Uri("http://localhost:49423");//地址
                HttpClient clien = new HttpClient();
                clien.BaseAddress = uri;
                clien.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpContent cotent = new StringContent(JsonConvert.SerializeObject(mm));

                cotent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                HttpResponseMessage message = clien.PutAsync("api/show/update", cotent).Result;

                if (message.IsSuccessStatusCode)
                {
                    var data = message.Content.ReadAsStringAsync().Result;

                    if (Convert.ToInt32(data) > 0)
                    {
                        MessageBox.Show("修改成功");
                        this.Close();
                    }
                }

            


        }

  • 相关阅读:
    并查集的来龙去脉
    改变像素
    PHP自学之路-----javascript基础入门
    推荐管理类图书
    ewebeditor编辑器ASP/ASPX/PHP/JSP版本漏洞利用总结及解决方法
    PHP文件包含漏洞剖析
    文件上传漏洞总结
    服务器软件解析漏洞总结
    JAVA环境变量配置
    Python2.x与Python3.x的区别
  • 原文地址:https://www.cnblogs.com/yxlx/p/7861308.html
Copyright © 2020-2023  润新知