zoukankan      html  css  js  c++  java
  • c# 模拟POST上传文件到服务器

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Text;
    using System.Net;
    
    namespace HuaTong.General.Utility
    {
        /// <summary>
        /// 用于模拟POST上传文件到服务器
        /// </summary>
        public class HttpUpload
        {
            private ArrayList bytesArray;
            private Encoding encoding = Encoding.UTF8;
            private string boundary = String.Empty;
    
            public HttpUpload()
            {
                bytesArray = new ArrayList();
                string flag = DateTime.Now.Ticks.ToString("x");
                boundary = "---------------------------" + flag;
            }
    
            /// <summary>
            /// 合并请求数据
            /// </summary>
            /// <returns></returns>
            private byte[] MergeContent()
            {
                int length = 0;
                int readLength = 0;
                string endBoundary = "--" + boundary + "--
    ";
                byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
    
                bytesArray.Add(endBoundaryBytes);
    
                foreach (byte[] b in bytesArray)
                {
                    length += b.Length;
                }
    
                byte[] bytes = new byte[length];
    
                foreach (byte[] b in bytesArray)
                {
                    b.CopyTo(bytes, readLength);
                    readLength += b.Length;
                }
    
                return bytes;
            }
    
            /// <summary>
            /// 上传
            /// </summary>
            /// <param name="requestUrl">请求url</param>
            /// <param name="responseText">响应</param>
            /// <returns></returns>
            public bool Upload(String requestUrl, out String responseText)
            {
                WebClient webClient = new WebClient();
                webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
    
                byte[] responseBytes;
                byte[] bytes = MergeContent();
    
                try
                {
                    responseBytes = webClient.UploadData(requestUrl, bytes);
                    responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
                    return true;
                }
                catch (WebException ex)
                {
                    Stream responseStream = ex.Response.GetResponseStream();
                    responseBytes = new byte[ex.Response.ContentLength];
                    responseStream.Read(responseBytes, 0, responseBytes.Length);
                }
                responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
                return false;
            }
    
            /// <summary>
            /// 设置表单数据字段
            /// </summary>
            /// <param name="fieldName">字段名</param>
            /// <param name="fieldValue">字段值</param>
            /// <returns></returns>
            public void SetFieldValue(String fieldName, String fieldValue)
            {
                string httpRow = "--" + boundary + "
    Content-Disposition: form-data; name="{0}"
    
    {1}
    ";
                string httpRowData = String.Format(httpRow, fieldName, fieldValue);
    
                bytesArray.Add(encoding.GetBytes(httpRowData));
            }
    
            /// <summary>
            /// 设置表单文件数据
            /// </summary>
            /// <param name="fieldName">字段名</param>
            /// <param name="filename">字段值</param>
            /// <param name="contentType">内容内型</param>
            /// <param name="fileBytes">文件字节流</param>
            /// <returns></returns>
            public void SetFieldValue(String fieldName, String filename, String contentType, Byte[] fileBytes)
            {
                string end = "
    ";
                string httpRow = "--" + boundary + "
    Content-Disposition: form-data; name="{0}"; filename="{1}"
    Content-Type: {2}
    
    ";
                string httpRowData = String.Format(httpRow, fieldName, filename, contentType);
    
                byte[] headerBytes = encoding.GetBytes(httpRowData);
                byte[] endBytes = encoding.GetBytes(end);
                byte[] fileDataBytes = new byte[headerBytes.Length + fileBytes.Length + endBytes.Length];
    
                headerBytes.CopyTo(fileDataBytes, 0);
                fileBytes.CopyTo(fileDataBytes, headerBytes.Length);
                endBytes.CopyTo(fileDataBytes, headerBytes.Length + fileBytes.Length);
    
                bytesArray.Add(fileDataBytes);
            }
        }
    }
  • 相关阅读:
    用户界面线程(含有消息泵的线程)主线程与用户界面线程的通信
    poj 2151 Check the difficulty of problems 概率dp
    Codeforces 148D Bag of mice 概率dp
    [置顶] UVa在线比赛单题汇总DP专题
    UVa 10405 Longest Common Subsequence 最长公共子序列模板
    UVa 12018 Juice Extractor 切水果dp暂时存疑
    UVa 674 Coin Change 背包dp
    hdu 3853 LOOPS 圆环之理 概率dp
    UVa 111 History Grading 最长递增子序列 LIS
    UVa在线比赛单题汇总DP专题
  • 原文地址:https://www.cnblogs.com/password1/p/5870725.html
Copyright © 2011-2022 走看看