欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#使用post方式提交json数据

程序员文章站 2022-08-29 09:38:07
尝试了一天,尝试了各种方法,一下方法最直接方便. ......

尝试了一天,尝试了各种方法,一下方法最直接方便. 

//地址
            string _url = "https://www.dxxxayup.ink/api/user/login";
            //json参数
            string jsonparam = "{ phonenumber:\"18665885202\",pwd:\"tsp\"}";
            var request = (httpwebrequest)webrequest.create(_url);
            request.method = "post";
            request.contenttype = "application/json;charset=utf-8";
            byte[] bytedata = encoding.utf8.getbytes(jsonparam);
            int length = bytedata.length;
            request.contentlength = length;
            stream writer = request.getrequeststream();
            writer.write(bytedata, 0, length);
            writer.close();
            var response = (httpwebresponse)request.getresponse();
            var responsestring = new streamreader(response.getresponsestream(), encoding.getencoding("utf-8")).readtoend();
            messagebox.show(responsestring.tostring());