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 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> winform c# Http上传数据 带参数 -> 正文阅读

[网络协议]winform c# Http上传数据 带参数

using System.Net.Http;
using System;
using System.Threading;
using Road_Test.Road.Tools.Singleton_Tool;
using Road_Test.Road.Log.Log_msg;

namespace Road_Test.Road.Tools.HttpUpload_Tool
{
    class HttpUploadTool:Singleton<HttpUploadTool>
    {
        private const string url = @"https://mclasstest.club/roadTestApi/receiveFile";
        // public const int MaxFiledataLimit = 10485760; // 10M
        // public const int MaxFiledataLimit = 5242880; // 5M
        public const int MaxFiledataLimit = 1048576; // 1M
        // public const int MaxFiledataLimit = 524288; //512
        // public const int MaxFiledataLimit = 242144; //256

        public Form1 Form1;
       
       
        // 历史记录
        public  string dwFullPath = null;
        public  string snrFullPath = null;
        public  string startTime = null;
        public  string endTime = null;
        public  string deviceId = null;

        private  string HttpdwFullPath = null;
        private  string HttpsnrFullPath = null;
        private  string HttpstartTime = null;
        private  string HttpendTime = null;
        private  string HttpdeviceId = null;

        private  Thread thread;
        public  void UploadFileHttpRequest()
        {
            HttpdwFullPath = dwFullPath;
            HttpsnrFullPath = snrFullPath;
            HttpstartTime = startTime;
            HttpendTime = endTime;
            HttpdeviceId = deviceId;
            dwFullPath = null;
            snrFullPath = null;

            ThreadStart thStart = new ThreadStart(StartUploadFileHttpRequest);//threadStart委托 
            thread = new Thread(thStart);
            thread.Priority = ThreadPriority.Highest;
            thread.IsBackground = true; //关闭窗体继续执行
            thread.Start();
            // 挂起线程
            // thread.Suspend();
            //继续执行线程
            // thread.Resume();
            //try
            //{
            //    //使用异常结束线程
            //    thread.Abort();
            //}
            //catch (Exception ex)
            //{
            //}
        }

        public  void StartUploadFileHttpRequest()
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    var content = new MultipartFormDataContent();
                    // 设置参数 类型为type 参数为 dw
                    content.Add(new StringContent("dw"), "type");
                    content.Add(new StringContent(HttpstartTime), "startTime");
                    content.Add(new StringContent(HttpendTime), "endTime");
                    content.Add(new StringContent(HttpdeviceId), "设备id");
                    // 上传文件类型 
                    content.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(HttpdwFullPath)), "dwFile", System.IO.Path.GetFileName(HttpdwFullPath));
                    content.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(HttpsnrFullPath)), "snrFile", System.IO.Path.GetFileName(HttpsnrFullPath));

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

                    Form1.ShowData(result);
                }
                catch (Exception ex)
                {
                    LogMSG.WriteLogmsg("Http数据上传异常", ex.Message.ToString());
                }
                
                try
                {
                    //使用异常结束线程
                    thread.Abort();
                }
                catch (Exception)
                {
                    // 这里通过异常结束调异步

                }
            }
            
        }
    }

   
}

namespace Road_Test.Road.Tools.Singleton_Tool
{
    public  class Singleton<T> where T : new()
    {
        private static T _instance;
    
        public static T Instance
        {
            get
            {
                if (_instance==null)
                {
                    _instance = new T();
                }

                return _instance;
            }
        }
    }
}

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-11-16 19:11:36  更:2021-11-16 19:13:29 
 
开发: 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年7日历 -2024/7/3 20:50:04-

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