• C#底层库–网络通信帮助类HTTP


    系列文章

    C#底层库–记录日志帮助类
    本文链接:https://blog.csdn.net/youcheng_ge/article/details/124187709

    C#底层库–数据库访问帮助类(MySQL版)
    本文链接:https://blog.csdn.net/youcheng_ge/article/details/126886379

    C#底层库–获取文件版本和MD5值
    本文链接:https://blog.csdn.net/youcheng_ge/article/details/112513871

    C#底层库–操作文件帮助类FileHelper(获取目录的所有文件)
    本文链接:https://blog.csdn.net/youcheng_ge/article/details/126887161

    C#底层库–操作Excel帮助类(读取、导出表格)
    本文链接:https://blog.csdn.net/youcheng_ge/article/details/126887445

    C#底层库–软件版本管理XML
    本文链接:https://blog.csdn.net/youcheng_ge/article/details/110195766

    C#底层库–随机数生成类
    本文链接:https://blog.csdn.net/youcheng_ge/article/details/126888812

    C#RegexHelper正则表达式帮助类
    本文链接:https://blog.csdn.net/youcheng_ge/article/details/109745286


    前言

    文将新增一个专栏–底层库,分享编程过程中常用的方法函数。我们将这些常用的方法函数,进行封装,反复测试,形成通用化类库。
    方便研发人员,只需要几行代码就可以使用它,解决一些难点问题。
    底层库的封装涉及到:数据库操作、加解密算法、日志记录、网络通信、邮件发送、文件操作、参数保存、Excel导入导出等等,持续关注本专栏吧。大家有任何问题,也可以评论区反馈,私信我。

    一、底层库介绍

    网络访问帮助类,采用HTTP方式访问服务端,包含get、post、put、delete。

    二、底层库源码

    创建类HttpHelper.cs,复制以下代码

    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    
    namespace QRCodeProduce.BLL
    {
        public class HttpHelper
        {
            /// 
            /// Get方法
            /// 
            /// ?a_fileName=20220908-2.apk
            /// url
            /// 
            public static string HttpGet(string Url, string a_ParamData)
            {
                try
                {
                    byte[] byteArray = Encoding.UTF8.GetBytes(a_ParamData);
                    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
                    webRequest.Method = "GET";
                    webRequest.Accept = "application/json, text/javascript, */*";  // 出错就删掉
                    webRequest.ContentType = "application/json; charset=utf-8";
                    webRequest.ContentLength = byteArray.Length;
    
                    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                    {
                        return sr.ReadToEnd(); // 返回的数据
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
    
            /// 
            /// Post请求
            /// 
            /// 
            /// { "no": "A28A1MW43C"}
            /// 
            public static string HttpPost(string url, string body)
            {
                try
                {
                    byte[] byteArray = Encoding.UTF8.GetBytes(body);
                    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                    webRequest.Method = "POST";
                    webRequest.Accept = "application/json, text/javascript, */*";
                    webRequest.ContentType = "application/json; charset=utf-8";
                    webRequest.ContentLength = byteArray.Length;
                    webRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length);
    
                    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                    {
                        return sr.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
    
            /// 
            /// Put请求
            /// 
            /// URL
            /// application/json
            /// 
            public static string HttpPut(string url, string body)
            {
                try
                {
                    byte[] byteArray = Encoding.UTF8.GetBytes(body);
                    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                    webRequest.Method = "PUT";
                    webRequest.Accept = "application/json, text/javascript, */*";
                    webRequest.ContentType = "application/json";
                    webRequest.ContentLength = byteArray.Length;
                    webRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length);
    
                    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                    {
                        return sr.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
    
            /// 
            /// Delete请求
            /// 
            /// URL
            /// application/json
            /// 
            public static string HttpDelete(string url, string body)
            {
                try
                {
                    byte[] byteArray = Encoding.UTF8.GetBytes(body);
                    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                    webRequest.Method = "DELETE";
                    webRequest.Accept = "application/json, text/javascript, */*";
                    webRequest.ContentType = "application/json";
                    webRequest.ContentLength = byteArray.Length;
                    webRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length);
    
                    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                    {
                        return sr.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131

    三、调用方法

    我这里是写了一个button按钮,扫码获得编号,然后点击读取标签,直接可以获得数据。

      private void BTN_Scan_Click(object sender, EventArgs e)
            {
                ScanModel model = new ScanModel
                {
                    no = baseDataInput_原料编号.StringValue
                }; 
           
                string l_strYuanLiaoBianhao = JsonConvert.SerializeObject(model);
    
                string l_strReturn = HttpHelper.HttpPost(string.Format(Const.ct_strScanHSLabel, AppConfig.GetValue("db_server")), l_strYuanLiaoBianhao);
    
                ActionResult Result = JsonConvert.DeserializeObject<ActionResult>(l_strReturn);
    
                if (Result.RetInfo.IsSUCD)
                {
                    Dictionary<string, string> dicResult = new Dictionary<string, string>();
                    dicResult.Clear();
    
                    JObject jo1 = (JObject)JsonConvert.DeserializeObject(Result.Data.Obj.ToString());//JSON反序列化
                    foreach (var item in jo1)
                    {
                        dicResult.Add(item.Key, item.Value.ToString());
                    }
    
                    baseDataInput_原料规格.StringValue = dicResult["黄丝规格"];
                    baseDataInput_原料强度.StringValue = dicResult["强度代号"];
                    baseDataInput_黄丝厂家.StringValue = dicResult["黄丝厂家"];
                    baseDataInput_黄丝代码.StringValue = dicResult["黄丝代码"];
                    baseDataInput_doff.StringValue = dicResult["doff"];
                }
                else
                {
                    XtraMessageBox.Show(Result.RetInfo.ErrorCode + ":" + Result.RetInfo.ErrorMsg,
                     "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    数据结构ScanModel.cs

        public class ScanModel
        {
            public string no { get; set; }
        }
    
    • 1
    • 2
    • 3
    • 4

    四、运行效果

    PC连接扫码枪,扫描一维码、二维码标签,自动复制“原料编号”,点击“读标”按钮自动获取标签信息。
    在这里插入图片描述

  • 相关阅读:
    【JavaEE-Servlet】Filter过滤器详解
    C++代码编程学习:inline函数学习(Essential C++ 第二章)
    c语言练习96:输入任意正整数n,显示出1到n之间的所有完数输出。
    6163. 给定条件下构造矩阵——每日一难(phase2_day1)
    《信阳师范学院学报(自然科学版)》
    Keepalived+Nginx+Tomcat配置高可用负载均衡系统示例
    如何使用 NFTScan NFT API 在 Arbitrum 网络上开发 Web3 应用
    MATLAB 运算符
    JAVA IO——常用的类
    笔记_前端基础试题-面试前的准备
  • 原文地址:https://blog.csdn.net/youcheng_ge/article/details/126886697