Http post 提交数据 上传文件 辅助类

 用于客户端上传文件和提交数据

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Net.Http
{
    public class UploadTools
    {
        public static void Post(string url, string filename)
        {
            Post(url, filename, System.IO.File.ReadAllBytes(filename), null);
        }

        public static void Post(string url, string filename, Dictionary<string, string> paramsvalue)
        {
            Post(url, filename, System.IO.File.ReadAllBytes(filename), paramsvalue);
        }

        public static void Post(string url, Dictionary<string, string> paramsvalue)
        {
            Post(url, string.Empty, null, paramsvalue);
        }
        public static void Post(string url, string key, string value)
        {
            Dictionary<string, string> paramsvalue = new Dictionary<string, string>();
            paramsvalue.Add(key, value);
            Post(url, string.Empty, null, paramsvalue);
        }

        public static void Post(string url, string filename, byte[] filedata, Dictionary<string, string> paramsvalue)
        {

            string boundary = "Upload_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string Enter = "\r\n";
             
            string itemfile = "--" + boundary + Enter
                    + "Content-Type:application/octet-stream" + Enter
                    + "Content-Disposition: form-data; name=\"fileContent\"; filename=\"" + filename + "\"" + Enter + Enter;

            StringBuilder sb = new StringBuilder();
            foreach (var key in paramsvalue)
            {
                string item = Enter + "--" + boundary + Enter
                        + "Content-Disposition: form-data; name=\"" + key.Key + "\"" + Enter + Enter
                        + key.Value + Enter;
                sb.Append(item);
            }
            string keyvalue = sb.ToString();
            string endstr= "--" + boundary + "--";


            byte[] itemfiledata = Encoding.UTF8.GetBytes(itemfile);
            byte[] itemdata = Encoding.UTF8.GetBytes(keyvalue);

            var endata = Encoding.UTF8.GetBytes(endstr); 
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "multipart/form-data;boundary=" + boundary;

            Stream requeststream = request.GetRequestStream();

            if (paramsvalue.Count < 1)
            {
                requeststream.Write(itemfiledata, 0, itemfiledata.Length);
                requeststream.Write(endata, 0, endata.Length);
                requeststream.Write(filedata, 0, filedata.Length);
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(filename))
                {
                    requeststream.Write(itemfiledata, 0, itemfiledata.Length);
                    requeststream.Write(filedata, 0, filedata.Length);
                }
                requeststream.Write(itemdata, 0, itemdata.Length);
                requeststream.Write(endata, 0, endata.Length);
            }

             
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responsestream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(responsestream, Encoding.GetEncoding("utf-8"));
            string result = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            responsestream.Close();
        }
 
    }
}

c# ASP.NET WEBFORM 服务器端

    public partial class UpLoadFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            string path = ConfigTools.GetKey("UploadSavePath", string.Empty);
            if (string.IsNullOrEmpty(path))
            {
                path = MapPath("\\File\\");
            }
            string name = Request["updateTime"];
            if (Request.Files.Count > 0)
            {
                HttpPostedFile file = Request.Files[0];
                string filename = path + DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetFileName(file.FileName);
                file.SaveAs(filename);
            }
            Response.Write("Success\r\n");
             
        }
    }

测试代码:

        private void button6_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> dics = new Dictionary<string, string>();
            dics.Add("updateTime", "aaaaa");
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.ShowDialog();
            UploadTools.Post("http://localhost:8888/testproject/Pages/UpLoadFile.aspx", dlg.FileName, dics);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> dics = new Dictionary<string, string>();
            dics.Add("updateTime", "aaaaa");
            UploadTools.Post("http://localhost:8888/testproject/Pages/UpLoadFile.aspx", dics);
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值