zoukankan      html  css  js  c++  java
  • .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:UsersPublicPicturesSample Picturesaa.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("
    --" + boundary + "--
    ");
                postDataStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
    
                return postDataStream;
            }
    View Code
  • 相关阅读:
    CF终于上紫了。。。
    CF567F/51nod2522 上下序列
    bzoj 前100题计划
    CF1110G Tree-Tac-Toe 博弈论、构造
    BZOJ4816 SDOI2017 数字表格 莫比乌斯反演
    UOJ400/LOJ2553 CTSC2018 暴力写挂 边分治、虚树
    Luogu4774 NOI2018 屠龙勇士 ExCRT
    CF1039D You Are Given a Tree 根号分治、二分、贪心
    CF1056E Check Transcription 字符串哈希
    Luogu4345 SHOI2015 超能粒子炮·改 Lucas、数位DP
  • 原文地址:https://www.cnblogs.com/wangxm123/p/4192192.html
Copyright © 2011-2022 走看看