3

I need a little help setting up a HTTP Post in C#. I appreciate any assistance I receive in advance.

Using Fiddler here is my 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 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.

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;
3
  • It seems to me there is a better way to do this. Can you enlighten us as to what you are trying to accomplish? (for example, upload a file) Commented Jun 18, 2012 at 21:19
  • 2
    I'm just trying to send a XML Post to a Banking provider. Everything is over complicated in that industry. Commented Jun 18, 2012 at 21:28
  • +1 for "Everything is over complicated in that industry" Commented Jun 18, 2012 at 21:29

1 Answer 1

6

I blogged about this and presented a sample method that could be used to send multipart/form-data requests. Checkout here: http://www.bratched.com/en/home/dotnet/69-uploading-multiple-files-with-c.html

Sign up to request clarification or add additional context in comments.

1 Comment

Darin, that seems to help me get a little closer to what is needed. I will play with it a bit more and keep this open for another day and mark your answer as acceptable. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.