IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> c#http get post 请求 -> 正文阅读

[网络协议]c#http get post 请求

		/// <summary>
        /// GET请求与获取结果
        /// </summary>
        public static string HttpGet(string Url, string postDataStr)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                request.Timeout = 10000;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }
            catch (Exception)
            {
                return null;
            }
        }
       	/// <summary>
        /// GET请求 带有 头信息
        /// </summary>
		public static string HttpGetList(string Url, string token)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "GET";
                request.Headers.Add("x-token", token);
                request.ContentType = "text/html;charset=UTF-8";
                request.Timeout = 10000;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// POST请求与获取结果
        /// </summary>
        public static string HttpPost(string Url, string postDataStr)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.Timeout = 6000000;
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postDataStr.Length;
                StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
                writer.Write(postDataStr);
                writer.Flush();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码
                }
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string retString = reader.ReadToEnd();
                return retString;
            }
            catch (Exception)
            {
                return null;
            }
        }
  /// <summary>
        /// POST请求与获取结果 带有头信息
        /// </summary>
        public static string HttpPostObj(string Url ,string token,string modelName, string model, string color)
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    var content = new MultipartFormDataContent();
                    content.Headers.Add("x-token", token);
                    content.Add(new StringContent(modelName), "modelName");
                    content.Add(new ByteArrayContent(File.ReadAllBytes(model)), "model",Path.GetFileName(model));
                    content.Add(new ByteArrayContent(File.ReadAllBytes(color)), "color", Path.GetFileName(color));

                    var result = client.PostAsync(Url, content).Result.Content.ReadAsStringAsync().Result;

                    return result;
                }
                catch (Exception ex)
                {
                    LogMSG.WriteLogmsg("Http数据上传异常", ex.Message.ToString());
                }
            }

            return null;
        }


代码结果解析

 class JsonTools
    {
        public static Dictionary<string, string> LoginJsonTostr(string json)
        {
            Dictionary<string, string> loginmess = new Dictionary<string, string>();
            JObject jo = (JObject)JsonConvert.DeserializeObject(json);
            var status =  jo["status"] ;
            loginmess.Add("status", status.ToString());

            var message = jo["message"];
            loginmess.Add("message", message.ToString());
            if (status.ToString()!="200")
            {
                return loginmess;
            }
            var data = jo["data"] as JObject;
            var xxx= data["xxx"];
            loginmess.Add("xxx", xxx.ToString()); 
            var xxx= data["xxx"];
            loginmess.Add("xxx", xxx.ToString());
            var xxx= data["xxx"];
            loginmess.Add("xxx", xxx.ToString());
            var xxx= xxx["xx"];
            loginmess.Add("xxxx", xxx.ToString());
            var xxx= xxx["xxxx"];
            loginmess.Add("xxxxx", xxx.ToString());
            var login_count = xxx["xxxxx"];
            loginmess.Add("xxxx", xxx.ToString());
            var xxx= xxx["xxxx"];
            loginmess.Add("xxxxx", xxx.ToString());
            var xxx= xxx["xxxx"];
            loginmess.Add("xxxx", xxx.ToString());
            var xxxx= admin["xxx"];
            loginmess.Add("xxxx", xxx.ToString());
            var xxxx= admin["xxx"];
            loginmess.Add("xxx", xxx.ToString());
            var xxx= admin["xxx"];
            loginmess.Add("xxx", xxx.ToString());
            return loginmess;
        }
        public static Dictionary<string, string> SendJsonTostr(string json)
        {
            Dictionary<string, string> Sendmess = new Dictionary<string, string>();
            JObject jo = (JObject)JsonConvert.DeserializeObject(json);
            var status = jo["status"];
            Sendmess.Add("status", status.ToString());

            var message = jo["message"];
            Sendmess.Add("message", message.ToString());

            return Sendmess;
        }
        public static string JsonTostr(string json)
        {
            JObject jo = (JObject)JsonConvert.DeserializeObject(json);
            var data = jo["data"] as JObject;
            string Cont = data["token"].ToString();
            return Cont;
        }

        public static List<mUserUpperlist> upplistJsonTostr(string json)
        {
            List<mUserUpperlist> mUserUpperlists = new List<mUserUpperlist>();
            JObject jo = (JObject)JsonConvert.DeserializeObject(json);
            var status = jo["status"];
            var message = jo["message"];
            if (status.ToString()!="200") // 如果失败直接返回 状态码 和信息 
            {
                mUserUpperlist muserUpperlist = new mUserUpperlist();
                muserUpperlist.status = status.ToString();
                muserUpperlist.message = message.ToString();
                mUserUpperlists.Add(muserUpperlist);
                return mUserUpperlists;
            }
            var data = jo["data"];
            var xxx= data["xxx"];
            var xxx= data["xxx"];
            int len= Convert.ToInt32(count.ToString());
            for (int i = 0; i < len-1; i++)
            {
                mUserUpperlist muserUpperlist = new mUserUpperlist();
                muserUpperlist.status = status.ToString();
                muserUpperlist.message = message.ToString();
                muserUpperlist.count = count.ToString();
               
                var xxx= list[i]["xxx"];
                var xxx= list[i]["xxx"];
                var xxx= list[i]["xxx"];
                var xxx= list[i]["xxx"];
                var xxx= list[i]["xxx"];
                var xxx= list[i]["xxx"];
                var xxx= list[i]["xxxx"];
                var xxx= list[i]["xxx"];
                muserUpperlist.mlist = new Dictionary<string, string>();
                var create_time = list[i]["create_time"];
                muserUpperlist.mlist.Add("xxx", xxx.ToString());
                muserUpperlist.mlist.Add("xxx", xxx.ToString());
                muserUpperlist.mlist.Add("xxx", xxx.ToString());
                muserUpperlist.mlist.Add("xxx", xxx.ToString());
                muserUpperlist.mlist.Add("xxx", xxx.ToString());
                muserUpperlist.mlist.Add("xxx", xxxx.ToString());
                muserUpperlist.mlist.Add("xxx", xxx.ToString());
                muserUpperlist.mlist.Add("xxx", xxx.ToString());
                muserUpperlist.mlist.Add("xxxx", xxxx.ToString());
                mUserUpperlists.Add(muserUpperlist);
            }
            return mUserUpperlists;
           
        }
    }

user 类

 class mUser
    {
        public static string account;
        public static string password;

        public static Dictionary<string, string> loginmess;
        public static Dictionary<string, string> Sendmess;
        public static List<mUserUpperlist> mUserUpperlists;
 
    }
    public struct mUserUpperlist
    {
        public string status;
        public string message;
        public string count;
        public Dictionary<string, string> mlist;

    }
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-04-22 19:15:05  更:2022-04-22 19:18:35 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 3:40:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码