使用 HttpWebRequest 的 HTTP POST

发布于 2024-11-07 13:52:24 字数 7063 浏览 0 评论 0原文

我正在尝试发布一些数据,就像我在 HTML 网站上使用 FORM 一样(ContentType = multipart/form-data)。目标是亚马逊的s3。 我正在使用 HttpWebRequest / HttpWebResponse,一切对我来说似乎都很好,但我仍然无法解决一个问题,我不断收到错误:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
  <Code>InvalidArgument</Code>
  <Message>POST requires exactly one file upload per request.</Message>
  <ArgumentValue>0</ArgumentValue>
  <ArgumentName>file</ArgumentName>
</Error>

它似乎是自我描述的,但我确实发送了“文件”字段。我已经成功地通过测试网站发送了一个没有问题的文件,整个后期数据似乎完全相同。 当我通过wireshark“监视”请求时,所有标头和发布数据均已按预期出现。

有谁知道为什么亚马逊看不到“文件”字段?

使用的代码(为了使其更具可读性而进行了一些删减):

private void addStringToStream(string buff, Stream s)
{
    var bytes = Encoding.UTF8.GetBytes(buff);
    s.Write(bytes, 0, bytes.Length);

    counter += bytes.Length;
}

private void doPost()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

    request.Method = "POST";
    request.ProtocolVersion = new Version(1, 1);

    request.KeepAlive = true;
    request.AllowAutoRedirect = true;

    request.Headers.Add("Accept-Language", "en-us,en;q=0.5");
    request.Headers.Add("Accept-Encoding", "gzip,deflate");
    request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");

    request.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.10) Gecko/20071115 Firefox/2.0.0.10";

    string boundary = "---------------------------317205771417341028";
    request.ContentType = "multipart/form-data; boundary=" + boundary;

    request.SendChunked = false;
    request.Credentials = CredentialCache.DefaultCredentials;
    request.CookieContainer = new CookieContainer();

    request.ContentLength = calculateLength();

    using (Stream s = request.GetRequestStream())
    {
        boundary += "\n";

        string inLineContentH = "Content-Disposition: form-data; name=\"{0}\"";
        string contentH = "Content-Disposition: form-data; name=\"{0}\"" + "\n" + "\n";

        string keyH = "key";

        string aclH = "acl";
        string aclV = "public-read";

        string accessKeyH = "AWSAccessKeyId";
        string accessKeyV = publicKey;

        string policyH = "policy";
        string policyV = this.generatePolicy();

        string signatureH = "signature";
        string signatureV = generateSignature(policyV);

        string fileH = "file";
        string fileContentNameH = "filename=\"{0}\"";
        string contentType2H = "Content-Type: application/octet-stream";

        buff = boundary;
        addStringToStream(buff, s);

        buff = string.Format(contentH, keyH);
        addStringToStream(buff, s);

        buff = keyV + "\n";
        addStringToStream(buff, s);

        /* here are all of the others necessary fields added to stream just as the one above
        no other operations are used, just adding bytes to stream 
        and finally we get to the file: */

        buff = boundary;
        addStringToStream(buff, s);

        buff = string.Format(inLineContentH, fileH) + "; " + string.Format(fileContentNameH, Path.GetFileName(this.FilePath)) + "\n";
        addStringToStream(buff, s);

        buff = contentType2H + "\n" + "\n";
        addStringToStream(buff, s);

        var inStream = File.OpenRead(FilePath);
        int val;
        while ((val = inStream.ReadByte()) != -1)
        {
            s.WriteByte((byte)val);
            counter++;
        }

        buff = "\n";
        addStringToStream(buff, s);

        buff = boundary;
        addStringToStream(buff, s);

        buff = string.Format(contentH, "submit");
        addStringToStream(buff, s);

        buff = "Upload to Amazon S3" + "\n";
        addStringToStream(buff, s);

        buff = boundary.Replace("\r", "").Replace("\n", "") + "--";
        addStringToStream(buff, s);
    }

    request.GetResponse();
}

将数据发布到流中:

---------------------------317205771417341028
Content-Disposition: form-data; name="key"

webtest/image.png
---------------------------317205771417341028
Content-Disposition: form-data; name="acl"

public-read
---------------------------317205771417341028
Content-Disposition: form-data; name="AWSAccessKeyId"

AKIAJOQLTUC5H2NJ65NA
---------------------------317205771417341028
Content-Disposition: form-data; name="policy"

Oi8vd3d3Lmdvb2dsZS[...]c3QiXSwNCiAgXQ0KfQ0K
---------------------------317205771417341028
Content-Disposition: form-data; name="Signature"

06GBK5DJ71aB[...]M8Ct8JOE=
---------------------------317205771417341028
Content-Disposition: form-data; name="file"; filename="eclipse.png"
Content-Type: application/octet-stream

‰PNG

IHDRn
[...the rest of the image...]
IEND®B‚
---------------------------317205771417341028
Content-Disposition: form-data; name="submit"

Upload to Amazon S3
---------------------------317205771417341028--

并且...wireshark的列表:

OUT TCP 58383 > http [SYN] Seq=0 Win=8192 Len=0 MSS=1460 WS=2 SACK_PERM=1
IN  TCP http > 58383 [SYN, ACK] Seq=0 Ack=1 Win=8190 Len=0 MSS=1460 WS=6 SACK_PERM=1
OUT TCP 58383 > http [ACK] Seq=1 Ack=1 Win=65700 Len=0
OUT TCP [TCP segment of a reassembled PDU]
IN  HTTP    HTTP/1.1 100 Continue 
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
IN  TCP http > 58383 [ACK] Seq=26 Ack=2031 Win=1331456 Len=0
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
IN  TCP http > 58383 [ACK] Seq=26 Ack=3969 Win=1337344 Len=0
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
IN  TCP http > 58383 [ACK] Seq=26 Ack=6889 Win=1343232 Len=0
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
IN  TCP http > 58383 [ACK] Seq=26 Ack=8441 Win=1346048 Len=0
OUT HTTP    POST / HTTP/1.1
//the one above contains the last bytes added to the stream
IN  TCP http > 58383 [ACK] Seq=26 Ack=10225 Win=1348864 Len=0
IN  TCP http > 58383 [ACK] Seq=26 Ack=10857 Win=1351936 Len=0
IN  TCP http > 58383 [ACK] Seq=26 Ack=12162 Win=1354752 Len=0
IN  TCP http > 58383 [ACK] Seq=26 Ack=13622 Win=1357824 Len=0
IN  TCP http > 58383 [ACK] Seq=26 Ack=13913 Win=1360640 Len=0
IN  TCP [TCP segment of a reassembled PDU]
IN  HTTP/XML    HTTP/1.1 400 Bad Request 
//the one above includes the error info quoted in the beginning
IN  TCP http > 58383 [FIN, ACK] Seq=649 Ack=13913 Win=1360640 Len=0
OUT TCP 58383 > http [ACK] Seq=13913 Ack=650 Win=65052 Len=0
OUT TCP 58383 > http [FIN, ACK] Seq=13913 Ack=650 Win=65052 Len=0
IN  TCP http > 58383 [ACK] Seq=650 Ack=13914 Win=1360640 Len=0

上述报告与基于Web应用程序的上传生成的报告之间的唯一区别是Web应用程序based 具有较少的[重组 PDU 的 TCP 段]位置,并且具有一些[TCP Dup ACK 156#1] http > 58364 [ACK] Sq=1 Ack=7017 Win=11668 Len=0 条目(如果可能有帮助,我可以发送完整的跟踪)。

I'm trying to POST some data as if I was using FORM on HTML site (ContentType = multipart/form-data). The target is amazon's s3.
I'm using HttpWebRequest / HttpWebResponse and all seems kind of fine to me, but I still can't fight one problem, I keep getting the error:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
  <Code>InvalidArgument</Code>
  <Message>POST requires exactly one file upload per request.</Message>
  <ArgumentValue>0</ArgumentValue>
  <ArgumentName>file</ArgumentName>
</Error>

it seems to be self-describing, but I really do send the "file" field. I've managed to send a file with no problems through test web-site, the whole post-data seems to be exactly the same.
When I "spy" the request via wireshark all the headers and the post data as well - it's there as expected.

Does anyone have any idea, why amazon can not see "file" field?

code used (it's cut a bit to make it more readable):

private void addStringToStream(string buff, Stream s)
{
    var bytes = Encoding.UTF8.GetBytes(buff);
    s.Write(bytes, 0, bytes.Length);

    counter += bytes.Length;
}

private void doPost()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

    request.Method = "POST";
    request.ProtocolVersion = new Version(1, 1);

    request.KeepAlive = true;
    request.AllowAutoRedirect = true;

    request.Headers.Add("Accept-Language", "en-us,en;q=0.5");
    request.Headers.Add("Accept-Encoding", "gzip,deflate");
    request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");

    request.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.10) Gecko/20071115 Firefox/2.0.0.10";

    string boundary = "---------------------------317205771417341028";
    request.ContentType = "multipart/form-data; boundary=" + boundary;

    request.SendChunked = false;
    request.Credentials = CredentialCache.DefaultCredentials;
    request.CookieContainer = new CookieContainer();

    request.ContentLength = calculateLength();

    using (Stream s = request.GetRequestStream())
    {
        boundary += "\n";

        string inLineContentH = "Content-Disposition: form-data; name=\"{0}\"";
        string contentH = "Content-Disposition: form-data; name=\"{0}\"" + "\n" + "\n";

        string keyH = "key";

        string aclH = "acl";
        string aclV = "public-read";

        string accessKeyH = "AWSAccessKeyId";
        string accessKeyV = publicKey;

        string policyH = "policy";
        string policyV = this.generatePolicy();

        string signatureH = "signature";
        string signatureV = generateSignature(policyV);

        string fileH = "file";
        string fileContentNameH = "filename=\"{0}\"";
        string contentType2H = "Content-Type: application/octet-stream";

        buff = boundary;
        addStringToStream(buff, s);

        buff = string.Format(contentH, keyH);
        addStringToStream(buff, s);

        buff = keyV + "\n";
        addStringToStream(buff, s);

        /* here are all of the others necessary fields added to stream just as the one above
        no other operations are used, just adding bytes to stream 
        and finally we get to the file: */

        buff = boundary;
        addStringToStream(buff, s);

        buff = string.Format(inLineContentH, fileH) + "; " + string.Format(fileContentNameH, Path.GetFileName(this.FilePath)) + "\n";
        addStringToStream(buff, s);

        buff = contentType2H + "\n" + "\n";
        addStringToStream(buff, s);

        var inStream = File.OpenRead(FilePath);
        int val;
        while ((val = inStream.ReadByte()) != -1)
        {
            s.WriteByte((byte)val);
            counter++;
        }

        buff = "\n";
        addStringToStream(buff, s);

        buff = boundary;
        addStringToStream(buff, s);

        buff = string.Format(contentH, "submit");
        addStringToStream(buff, s);

        buff = "Upload to Amazon S3" + "\n";
        addStringToStream(buff, s);

        buff = boundary.Replace("\r", "").Replace("\n", "") + "--";
        addStringToStream(buff, s);
    }

    request.GetResponse();
}

post data that goes to the stream:

---------------------------317205771417341028
Content-Disposition: form-data; name="key"

webtest/image.png
---------------------------317205771417341028
Content-Disposition: form-data; name="acl"

public-read
---------------------------317205771417341028
Content-Disposition: form-data; name="AWSAccessKeyId"

AKIAJOQLTUC5H2NJ65NA
---------------------------317205771417341028
Content-Disposition: form-data; name="policy"

Oi8vd3d3Lmdvb2dsZS[...]c3QiXSwNCiAgXQ0KfQ0K
---------------------------317205771417341028
Content-Disposition: form-data; name="Signature"

06GBK5DJ71aB[...]M8Ct8JOE=
---------------------------317205771417341028
Content-Disposition: form-data; name="file"; filename="eclipse.png"
Content-Type: application/octet-stream

‰PNG

IHDRn
[...the rest of the image...]
IEND®B‚
---------------------------317205771417341028
Content-Disposition: form-data; name="submit"

Upload to Amazon S3
---------------------------317205771417341028--

and... the wireshark's list:

OUT TCP 58383 > http [SYN] Seq=0 Win=8192 Len=0 MSS=1460 WS=2 SACK_PERM=1
IN  TCP http > 58383 [SYN, ACK] Seq=0 Ack=1 Win=8190 Len=0 MSS=1460 WS=6 SACK_PERM=1
OUT TCP 58383 > http [ACK] Seq=1 Ack=1 Win=65700 Len=0
OUT TCP [TCP segment of a reassembled PDU]
IN  HTTP    HTTP/1.1 100 Continue 
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
IN  TCP http > 58383 [ACK] Seq=26 Ack=2031 Win=1331456 Len=0
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
IN  TCP http > 58383 [ACK] Seq=26 Ack=3969 Win=1337344 Len=0
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
IN  TCP http > 58383 [ACK] Seq=26 Ack=6889 Win=1343232 Len=0
OUT TCP [TCP segment of a reassembled PDU]
OUT TCP [TCP segment of a reassembled PDU]
IN  TCP http > 58383 [ACK] Seq=26 Ack=8441 Win=1346048 Len=0
OUT HTTP    POST / HTTP/1.1
//the one above contains the last bytes added to the stream
IN  TCP http > 58383 [ACK] Seq=26 Ack=10225 Win=1348864 Len=0
IN  TCP http > 58383 [ACK] Seq=26 Ack=10857 Win=1351936 Len=0
IN  TCP http > 58383 [ACK] Seq=26 Ack=12162 Win=1354752 Len=0
IN  TCP http > 58383 [ACK] Seq=26 Ack=13622 Win=1357824 Len=0
IN  TCP http > 58383 [ACK] Seq=26 Ack=13913 Win=1360640 Len=0
IN  TCP [TCP segment of a reassembled PDU]
IN  HTTP/XML    HTTP/1.1 400 Bad Request 
//the one above includes the error info quoted in the beginning
IN  TCP http > 58383 [FIN, ACK] Seq=649 Ack=13913 Win=1360640 Len=0
OUT TCP 58383 > http [ACK] Seq=13913 Ack=650 Win=65052 Len=0
OUT TCP 58383 > http [FIN, ACK] Seq=13913 Ack=650 Win=65052 Len=0
IN  TCP http > 58383 [ACK] Seq=650 Ack=13914 Win=1360640 Len=0

the only difference between above report and one generated by web-app based upload is that web-app based has less [TCP segment of a reassembled PDU] positions, and has some [TCP Dup ACK 156#1] http > 58364 [ACK] Sq=1 Ack=7017 Win=11668 Len=0 entries (if might be helpful I can send full traces).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄人书 2024-11-14 13:52:24

解决了!
边界错误 - 需要为发布数据添加比标头中的值多 2 个破折号的破折号。
上面的代码需要做的是:

string boundary = "---------------------------317205771417341028";
request.ContentType = "multipart/form-data; boundary=" + boundary;

[...]

using (Stream s = request.GetRequestStream())
{
    boundary = "--" + boundary "\n";

[...]
如此简单……但又如此烦人……

solved!
the boundary was wrong - it is needed to add 2 dashes more for post data than the value in the header has.
what was needed to do in above code was:

string boundary = "---------------------------317205771417341028";
request.ContentType = "multipart/form-data; boundary=" + boundary;

[...]

using (Stream s = request.GetRequestStream())
{
    boundary = "--" + boundary "\n";

[...]
so easy... and yet so annoying...

痕至 2024-11-14 13:52:24

我连续花了大约三天的时间试图解决同样的问题。请绝对确保您的 POST 正文格式使用一致的行结尾。我忘记了 CRLF 序列中的单个回车符 (\r),Amazon S3 给了我这个错误和类似的错误。

I spent about three days straight trying to solve the same issue. Make absolutely sure you use consistent line endings for your POST body format. I forgot a single carriage return (\r) in a CRLF sequence and Amazon S3 gave me this error and similar ones.

灵芸 2024-11-14 13:52:24

将其发布到您自己的服务器而不是亚马逊,然后检查 POST - 我敢打赌您的标头设置不正确,这意味着因为您使用的是多部分表单数据,所以 POST 无法解码。

无论如何,您应该使用官方 SDK。

http://aws.amazon.com/sdkfornet/

POST that to your own server not Amazon, and then check the POST - I will bet that your headers are not set correctly, meaning that because you are using multipart form-data, the POST can't be decoded.

You should use the official SDK anyway.

http://aws.amazon.com/sdkfornet/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文