0

HTTP POST2 つの変数 (ユーザー名とパスワード) とファイル (.zip) を含むリクエスト フォームを実行する次の html ページがあります。c#HTML の代わりにこの投稿を行うための Windows フォーム アプリケーションを作成したいと考えています。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