zoukankan      html  css  js  c++  java
  • c# 后台GET、POST、PUT、DELETE传输发送json数据

    一、Get 方式传输

         //url为请求的网址,param参数为需要查询的条件(服务端接收的参数,没有则为null)
            //返回该次请求的响应
            public string HttpGet(string url, Dictionary<String, String> param)
            {
                if (param != null) //有参数的情况下,拼接url
                {
                    url = url + "?";
                    foreach (var item in param)
                    {
                        url = url + item.Key + "=" + item.Value + "&";
                    }
                    url = url.Substring(0, url.Length - 1);
                }
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;//创建请求
                request.Method = "GET"; //请求方法为GET
                HttpWebResponse res; //定义返回的response
                try
                {
                    res = (HttpWebResponse)request.GetResponse(); //此处发送了请求并获得响应
                }
                catch (WebException ex)
                {
                    res = (HttpWebResponse)ex.Response;
                }
                StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                string content = sr.ReadToEnd(); //响应转化为String字符串
                return content;
            }

    二、POST 方式传输

    public static string HttpPost(string url, Dictionary<String, String> param)
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //创建请求
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                //request.AllowReadStreamBuffering = true;
                request.MaximumResponseHeadersLength = 1024;
                request.Method = "POST"; //请求方式为post
                request.AllowAutoRedirect = true;
                request.MaximumResponseHeadersLength = 1024;
                request.ContentType = "application/json";
                JObject json = new JObject();
                if (param.Count != 0) //将参数添加到json对象中
                {
                    foreach (var item in param)
                    {
                        json.Add(item.Key, item.Value);
                    }
                }
                string jsonstring = json.ToString();//获得参数的json字符串
                byte[] jsonbyte = Encoding.UTF8.GetBytes(jsonstring);
                Stream postStream = request.GetRequestStream();
                postStream.Write(jsonbyte, 0, jsonbyte.Length);
                postStream.Close();
                //发送请求并获取相应回应数据       
                HttpWebResponse res;
                try
                {
                    res = (HttpWebResponse)request.GetResponse();
                }
                catch (WebException ex)
                {
                    res = (HttpWebResponse)ex.Response;
                }
                StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                string content = sr.ReadToEnd(); //获得响应字符串
                return content;
            }

    其中PUT、DELETE方式跟上面基本相似。这里就不再多说明

  • 相关阅读:
    移动端适配方案总结
    排序算法
    使用onchange依赖监控文件修改失效
    实现一个可拖拽的div
    在vue中实现两个输入框内容的同步及转换
    简易loading动画的制作
    了解MVC
    Spring Boot使用模板引擎总结
    在配置好log4j后信息log还是在Console中输出
    运行时报java.sql.SQLException: No suitable driver的几种解决办法
  • 原文地址:https://www.cnblogs.com/wangyonglai/p/8327690.html
Copyright © 2011-2022 走看看