.net HttpWebRequest 模拟form提交

static void Main(string[] args)
        {
            //string URLAuth = "https://technet.rapaport.com/HTTP/Authenticate.aspx";
            //WebClient webClient = new WebClient();

           
            //formData["Username"] = "myUser";
            //formData["Password"] = "myPassword";

            //byte[] responseBytes = webClient.UploadValues(URLAuth, "POST", formData);
            //string resultAuthTicket = Encoding.UTF8.GetString(responseBytes);
            //webClient.Dispose();

            string URL = "http://localhost:8302/upload";
            string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
            System.Net.WebRequest webRequest = System.Net.WebRequest.Create(URL);

            webRequest.Method = "POST";
            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;

            string FilePath = @"C:\Users\Public\Pictures\Sample Pictures\aa.jpg";
            NameValueCollection formData = new NameValueCollection();
            formData["ReplaceAll"] = "false";

            Stream postDataStream = GetPostStream(FilePath, formData, boundary);

            webRequest.ContentLength = postDataStream.Length;
            Stream reqStream = webRequest.GetRequestStream();

            postDataStream.Position = 0;

            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            while ((bytesRead = postDataStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                reqStream.Write(buffer, 0, bytesRead);
            }

            postDataStream.Close();
            reqStream.Close();

            StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream());
            string Result = sr.ReadToEnd();
        }
View Code
private static Stream GetPostStream(string filePath, NameValueCollection formData, string boundary)
        {
            Stream postDataStream = new System.IO.MemoryStream();

            //adding form data
            string formDataHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine +
            "Content-Disposition: form-data; name=\"{0}\";" + Environment.NewLine + Environment.NewLine + "{1}";

            foreach (string key in formData.Keys)
            {
                byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(formDataHeaderTemplate,
                key, formData[key]));
                postDataStream.Write(formItemBytes, 0, formItemBytes.Length);
            }

            //adding file data
            FileInfo fileInfo = new FileInfo(filePath);

            string fileHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine +
            "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" +
            Environment.NewLine + "Content-Type: image/jpeg" + Environment.NewLine + Environment.NewLine;

            byte[] fileHeaderBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(fileHeaderTemplate,
            "UploadCSVFile", fileInfo.FullName));

            postDataStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);

            FileStream fileStream = fileInfo.OpenRead();

            byte[] buffer = new byte[1024];

            int bytesRead = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                postDataStream.Write(buffer, 0, bytesRead);
            }

            fileStream.Close();

            byte[] endBoundaryBytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
            postDataStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);

            return postDataStream;
        }
View Code

 

posted @ 2014-12-29 18:11  一切从简  阅读(2581)  评论(0编辑  收藏  举报