zoukankan      html  css  js  c++  java
  • C# Post Json数据

    public string Post(string Url, string jsonParas)
        {
            string strURL = Url;

            //创建一个HTTP请求 
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            //Post请求方式 
            request.Method = "POST";
            //内容类型
            request.ContentType = "application/x-www-form-urlencoded";

            //设置参数,并进行URL编码 

            string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas);   

            byte[] payload;
            //将Json字符串转化为字节 
            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            //设置请求的ContentLength  
            request.ContentLength = payload.Length;
            //发送请求,获得请求流 

            Stream writer;
            try
            {
                writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
            }
            catch (Exception)
            {
                writer = null;
                Console.Write("连接服务器失败!");
            }
            //将请求参数写入流
            writer.Write(payload, 0, payload.Length);
            writer.Close();//关闭请求流

            String strValue = "";//strValue为http响应所返回的字符流
            HttpWebResponse response;
            try
            {
                //获得响应流
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
            }

            Stream s = response.GetResponseStream();


            Stream postData = Request.InputStream;
            StreamReader sRead = new StreamReader(s);
            string postContent = sRead.ReadToEnd();
            sRead.Close();


            return postContent;//返回Json数据
        }


    //接收
      //获取post过来的json数据结构
                        Stream postData = Request.InputStream;
                        StreamReader sRead = new StreamReader(postData);
                        string postContent = sRead.ReadToEnd();
                        sRead.Close();

  • 相关阅读:
    Kafka 配置说明
    Ubuntu中网络配置问题
    vuecli全家桶搭建个人博客并迁移nuxtjs由后端服务渲染页面
    记录vim模式快捷键
    开发常用mac软件推荐(JetBrains IntelliJ IDEA 2019 for Mac, Parallels Desktop 15 for Mac,cleanmymac, MindNode 7 for mac 思维导图软件)
    gulp4 多页面项目管理打包(html, es6,less编译压缩版本控制)
    Adobe Photoshop 2021 for mac(PS 2020) v22.0.0中文激活破解版
    git 常用命令使用,git bash通用命令
    mac百度网盘破解版免费
    好久木来了,一直忙于项目太忙(tailan),今天讲讲vuecli3.0的使用
  • 原文地址:https://www.cnblogs.com/Andy-Blog/p/5666180.html
Copyright © 2011-2022 走看看