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
    {
        /// <summary>
        /// Get方法
        /// </summary>
        /// <param name="a_ParamData">?a_fileName=20220908-2.apk</param>
        /// <param name="Url">url</param>
        /// <returns></returns>
        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);
            }
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="body">{ "no": "A28A1MW43C"}</param>
        /// <returns></returns>
        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);
            }
        }

        /// <summary>
        /// Put请求
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="body">application/json</param>
        /// <returns></returns>
        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);
            }
        }

        /// <summary>
        /// Delete请求
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="body">application/json</param>
        /// <returns></returns>
        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);
            }
        }
    }

}

三、调用方法

我这里是写了一个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);
            }
        }

数据结构ScanModel.cs

    public class ScanModel
    {
        public string no { get; set; }
    }

四、运行效果

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

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花北城

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值