【问题标题】:C# HTTP POST with Boundary带边界的 C# HTTP POST
【发布时间】:2012-06-20 21:59:29
【问题描述】:

我需要一些帮助来设置 C# 中的 HTTP 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>

我的要求有点棘手。他们需要一个带有边界的多部分帖子。我不熟悉设置边界。如果有人可以提供帮助,我将不胜感激。

这是我的要求:

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)

所以我认为这应该是 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--

这是我用来创建 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;

【问题讨论】:

  • 在我看来有更好的方法来做到这一点。你能告诉我们你想要完成什么吗? (例如,上传文件)
  • 我只是想向银行服务提供商发送一个 XML Post。该行业的一切都过于复杂。
  • +1 表示“该行业的一切都过于复杂”

标签: c# httpwebrequest http-post


【解决方案1】:

我在博客上对此进行了介绍,并提出了一个示例方法,可用于发送multipart/form-data 请求。在这里结帐:http://www.bratched.com/en/home/dotnet/69-uploading-multiple-files-with-c.html

【讨论】:

  • 达林,这似乎帮助我更接近需要的东西。我会多玩一点,然后再开一天,并将您的答案标记为可以接受。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 2020-05-26
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多