0

我有以下 html 页面,该页面HTTP POST使用 2 个变量(用户名和密码)和一个文件(.zip)生成请求表单。我想编写一个c#Windows 窗体应用程序来代替 html 来完成这篇文章。我尝试了许多在 stackoverflow 或其他页面中找到的示例代码,但没有一个有效。我收到诸如“400 Bad Request”或“415 Unsupported Media Type”之类的错误。

  <form action="http://import.xe.gr/request/add" enctype="multipart/form-data" method="POST">
        <center>
            <div>
                <b>form</b>
            </div>
            </br>
            </br>
        <table>
            <tr>
                <td>Username (username)</td>
                <td><input type="text" name="username" size="30"></td>
            </tr>
            <tr>
                <td>Password (password)</td>
                <td><input type="text" name="password" size="30"></td>
            </tr>
            <tr>
                <td>file</td>
                <td><input type="file" name="file" size="40"></td>
            </tr>
            <tr>enter code here
                <td colspan="2" align="center"><input type="submit" value="Send File">
    </td>
            </tr>
        </table>
        </center>
    </form> 

这是我尝试过的最后一个示例代码:

  private void button2_Click(object sender, EventArgs e)
    {


        using (var stream1 = File.Open(Environment.CurrentDirectory + filePath, FileMode.Open, FileAccess.Read))
         {
            var files = new[] 
         {
             new UploadFile
             {
                      Name = "file",
                       Filename ="1.zip",
                     ContentType = "application/zip, application/octet-stream",
                     Stream = stream1
             }
          };

            var values = new NameValueCollection
                   {
                            { "username", username},
                            { "password", password },
                   };

                   byte[] result = UploadFiles(url, files, values);
                   Console.Write(result);
            }

    }


    public class UploadFile
    {
        public UploadFile()
        {
            ContentType = "application/octet-stream";
        }
        public string Name { get; set; }
        public string Filename { get; set; }
        public string ContentType { get; set; }
        public Stream Stream { get; set; }
    }


    public byte[] UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values)
    {
        var request = WebRequest.Create(address);
        request.Method = "POST";
        var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        boundary = "--" + boundary;

        using (var requestStream = request.GetRequestStream())
        {
            // Write the values
            foreach (string name in values.Keys)
            {
                var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
            }

            // Write the files
            foreach (var file in files)
            {
                var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
                requestStream.Write(buffer, 0, buffer.Length);
                file.Stream.CopyTo(requestStream);
                buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
            }

            var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
            requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
        }

        using (var response = request.GetResponse())
        using (var responseStream = response.GetResponseStream())
        using (var stream = new MemoryStream())
        {
            responseStream.CopyTo(stream);
            return stream.ToArray();
        }
    }

谁能告诉我我的代码有什么问题或以任何其他方式帮助我?

4

0 回答 0