【C#】使用C# 读取Http的Post数据

private string Post(string num)
        {

            Encoding myEncoding = Encoding.GetEncoding("gb2312");  //选择编码字符集
            string data = "customerSn=6fea75ba52gf339f&phone=" + num + "&cate=fuzhu&channel=hui-5";  //要上传到网页系统里的数据(字段名=数值 ,用&符号连接起来)
            byte[] bytesToPost = System.Text.Encoding.Default.GetBytes(data); //转换为bytes数据

            string responseResult = String.Empty;
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://95.163.204.90:2121/api/account/black.json");   //创建一个有效的httprequest请求,地址和端口和指定路径必须要和网页系统工程师确认正确,不然一直通讯不成功
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
            req.ContentLength = bytesToPost.Length;

            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bytesToPost, 0, bytesToPost.Length);     //把要上传网页系统的数据通过post发送
            }
            HttpWebResponse cnblogsRespone = (HttpWebResponse)req.GetResponse();
            if (cnblogsRespone != null && cnblogsRespone.StatusCode == HttpStatusCode.OK)
            {
                StreamReader sr;
                using (sr = new StreamReader(cnblogsRespone.GetResponseStream()))
                {
                    responseResult = sr.ReadToEnd();  //网页系统的json格式的返回值,在responseResult里,具体内容就是网页系统负责工程师跟你协议号的返回值协议内容
                }
                sr.Close();
            }
            cnblogsRespone.Close();
            return responseResult;
        }

  测试:Post("18868511332"); 返回:{"status":0,"msg":"ok","data":"18868511332"}

 

参考地址:https://www.cnblogs.com/da3j/p/10178627.html

posted on 2019-08-09 23:10  梦琪小生  阅读(3737)  评论(0编辑  收藏  举报

导航