简体   繁体   English

带边界的C#HTTP POST

[英]C# HTTP POST with Boundary

I need a little help setting up a HTTP Post in C#. 我需要一点帮助在C#中设置HTTP Post。 I appreciate any assistance I receive in advance. 我感谢您提前获得的任何帮助。

Using Fiddler here is my RAW POST: 在这里使用Fiddler是我的RAW POST:

POST http://www.domain.com/tester.aspx HTTP/1.1
User-Agent: Tegan
Content-Type: multipart/form-data; boundary=myboundary
Host: www.domain.com
Content-Length: 1538
Expect: 100-continue


<some-xml>

        <customer>
                <user-id>george</user-id>
                <first-name>George</first-name>
                <last-name>Jones</last-name>
        </customer>

</some-xml>

My requirements are a little tricky. 我的要求有点棘手。 They require a multi-part post with a boundary. 他们需要一个带有边界的多部分职位。 I'm not familiar with setting up a boundary. 我不熟悉设置边界。 If any one can assist I would appreciate it. 如果任何人可以提供帮助我会很感激。

Here are my requirements: 这是我的要求:

POST http://www.domain.com/tester.aspx HTTP/1.0(CRLF)
User-Agent: myprogramname(CRLF)
Content-Type: multipart/form-data; boundary=myboundary(CRLF)
Content-Length: nnn(CRLF)
(CRLF)
(CRLF)
--myboundary(CRLF)
Content-Disposition: form-data; name=”xmlrequest”(CRLF)
Content-Type: text/xml(CRLF)
(CRLF)
(XML request message)(CRLF)
(CRLF)
--myboundary--(CRLF)

So I think this is what the POST should look like but I need some help with my C#. 所以我认为这应该是POST应该是什么样子但我需要一些帮助我的C#。

POST http://www.domain.com/tester.aspx HTTP/1.1
User-Agent: Tegan
Content-Type: multipart/form-data; boundary=myboundary
Content-Length: 1538

--myboundary
Content-Disposition: form-data; name="xmlrequest"
Content-Type: text/xml

<some-xml>

            <customer>
                    <user-id>george</user-id>
                    <first-name>George</first-name>
                    <last-name>Jones</last-name>
            </customer>

    </some-xml>

(CRLF)
--myboundary--

Here is the C# code I'm using to create the WebRequest. 这是我用来创建WebRequest的C#代码。

HttpWebRequest request = null;
Uri uri = new Uri("http://domain.com/tester.aspx");
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.UserAgent = "NPPD";
request.ContentType = "multipart/form-data; boundary=myboundary";
request.ContentLength = postData.Length;


using (Stream writeStream = request.GetRequestStream())
{
    writeStream.Write(postData, 0, postData.Length);
}
string result = string.Empty;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    using (Stream responseStream = response.GetResponseStream())
    {
        using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
        {
            result = readStream.ReadToEnd();
        }
    }
}
return result;

I blogged about this and presented a sample method that could be used to send multipart/form-data requests. 我在博客上写了这个,并提供了一个可用于发送multipart/form-data请求的示例方法。 Checkout here: http://www.bratched.com/en/home/dotnet/69-uploading-multiple-files-with-c.html 在这里结帐: http//www.bratched.com/en/home/dotnet/69-uploading-multiple-files-with-c.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM