using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;namespace WinFormHttpPost
{
    public partial class Form1 : Form
    {
        private Encoding currentEncode = Encoding.GetEncoding("utf-8");        public Form1()
        {
            InitializeComponent();
        }        private void Submit(string url, string user, string pass, string filePath)
        {
            string boundary = Guid.NewGuid().ToString();
            string beginBoundary = "--" + boundary;
            string endBoundary = "--" + boundary + "--";            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.Method = "POST";
            request.KeepAlive = true;            StringBuilder sbBody = new StringBuilder();
            sbBody.AppendLine(beginBoundary);
            sbBody.AppendLine("Content-Disposition: form-data; name=\"user\"");
            sbBody.AppendLine();
            sbBody.AppendLine(user);            sbBody.AppendLine(beginBoundary);
            sbBody.AppendLine("Content-Disposition: form-data; name=\"pass\"");
            sbBody.AppendLine();
            sbBody.AppendLine(pass);            sbBody.AppendLine(beginBoundary);
            sbBody.AppendLine(string.Format("Content-Disposition: form-data; name=\"myFile\"; filename=\"{0}\"", filePath));
            sbBody.AppendLine("Content-Type: application/octet-stream");
            sbBody.AppendLine();            byte[] bufferContent = currentEncode.GetBytes(sbBody.ToString());
            byte[] bufferFile = GetFileByte(filePath);
            byte[] bufferEndBoundary = currentEncode.GetBytes("\r\n" + endBoundary);            byte[] bufferBody = new byte[bufferContent.Length + bufferFile.Length + bufferEndBoundary.Length];
            int startIndex = 0;
            bufferContent.CopyTo(bufferBody, startIndex);
            startIndex += bufferContent.Length;
            bufferFile.CopyTo(bufferBody, startIndex);
            startIndex += bufferFile.Length;
            bufferEndBoundary.CopyTo(bufferBody, startIndex);            request.ContentLength = bufferBody.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bufferBody, 0, bufferBody.Length);
            }            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(receiveStream, currentEncode);            tbResult.Text = readStream.ReadToEnd();
            response.Close();
            readStream.Close();
        }        private byte[] GetFileByte(string filePath)
        {
            byte[] bufferFileInfo = null;
            using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                bufferFileInfo = new byte[stream.Length];
                stream.Read(bufferFileInfo, 0, bufferFileInfo.Length);
            }            return bufferFileInfo;        }        private void btnSubmit_Click(object sender, EventArgs e)
        {
            Submit("http://localhost.:3558/Server.ashx", "abc", "123", "F:\\报表下载清单2.xls");
        }
    }
}