分享下C# winform的post提交

本文介绍了一个使用C#进行JSON序列化并利用HTTP POST请求与远程服务器交互的简单示例。示例中创建了一个名为`Jasonclass`的类来存储用户认证信息,并通过`JavaScriptSerializer`将其序列化为JSON格式。之后通过HTTP POST请求将JSON数据发送到指定URL,并从服务器获取响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public class Jasonclass
        {
            private string Mch_id;


            public string mch_id
            {
                get { return Mch_id; }
                set { Mch_id = value; }
            }
            private string User;


            public string user
            {
                get { return User; }
                set { User = value; }
            }
            private string Pass;


            public string pass
            {
                get { return Pass; }
                set { Pass = value; }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Jasonclass team = new Jasonclass()
            {
                mch_id="KK001",
                user="xiaogang",
                pass="123456"
            };
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            string json = js.Serialize(team);
            richTextBox1.Text = HttpPost("http://m.vwz.cn/fenrun.php/FrAPI/valid", "json=" + json);
        }
        public string HttpPost(string Url, string postDataStr)  //get读取
        {
            //发送
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);


            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] payload = System.Text.Encoding.UTF8.GetBytes(postDataStr);
            request.GetRequestStream().Write(payload, 0, payload.Length);
    


            //响应接收
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();


            Stream myResponseStream = response.GetResponseStream();


            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));


            string retString = myStreamReader.ReadToEnd();


            myStreamReader.Close();


            myResponseStream.Close();


            return retString;


        }


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值