C# post to different server

1.4k views Asked by At

I am using c# and trying to post data to a form on another server, the form includes an image attachment along with some parameters ex:

firstName = First lastName = Last Image = ImageFile.JPG

Looking for an example of how to construct a method to send this I've been looking into the HttpWebRequest but need an example.

2

There are 2 answers

0
eulerfx On

A simpler alternative to HttpWebRequest is the WebClient class. To upload name value pairs:

        using (var c = new WebClient())
        {
            var values = new NameValueCollection();
            values.Add("key1", "value1");
            c.UploadValues("http://www.acme.org/", "POST", values);
        }
0
James On

I have this working now - my test is with an ASP.NET hosted web page... but the basic technique should work anywhere.

Posting to web servers manually can get a bit tricky. I always start with a nice tool like HttpWatch - I make a real post to the page and then using the tool look at what it posted, and look at the headers sent, and look at the cookies sent.

In this case, the content type in the header is set to multipart/form-data and a boundary is defined. Then look at the stream and you can see how the bytes are being sent to the server.

Once those steps are done, you just have to figure out how to send the same bytes from c#.

This example code just shows one working example. Clearly, production code would take a few things a few steps farther:

1) It would connect first to obtain fresh __VIEWSTATE and __EVENTVALIDATION parameters as opposed to hardwiring them as I have here.

2) It would look at the form parameters (fname, lname) and confirm their names.

3) It might look for new for parameters.

4) It might generate boundry each time. Not sure on this one, but the browser does it this way.

5) The file name would not be hard wired.

6) There's probably a more efficient way to create the final byte array.

Here's the code:

    private void SimulatePost_Click(object sender, EventArgs e)
    {
        WebClient client = new WebClient();

        const string boundry = "---------------------------7dc2a722a12c8";

        string contentType = string.Format("multipart/form-data; boundary={0}", boundry);

        string postData = string.Format(@"--{0}
Content-Disposition: form-data; name=""__VIEWSTATE""

/wEPDwUKLTk2MDkyMzQyMw9kFgJmD2QWAgIDDxYCHgdlbmN0eXBlBRNtdWx0aXBhcnQvZm9ybS1kYXRhZGQLrqV5FQTzi8K9ogSJlS44c0L0Ou3+MaYfFPwjKPwjZQ==
--{0}
Content-Disposition: form-data; name=""__EVENTVALIDATION""

/wEWBAKXx6zsBALa1ffGCwK80PHQDQLurOv8AU7Jo8sYj9+E/zw7RsmFraAotTazyvQc7T2VseLqSwGO
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$fname""

jim
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$lname""

bob
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$picUpload""; filename=""C:\temp\small.JPG""
Content-Type: image/jpeg

", boundry);

        string endData = string.Format(@"
--{0}
Content-Disposition: form-data; name=""ctl00$MainContent$post""


--{0}--
", boundry);


        List<byte> postByteArray = Encoding.UTF8.GetBytes(postData).ToList();

        byte[] file = File.ReadAllBytes(@"C:\temp\small.JPG");

        postByteArray.AddRange(file);

        postByteArray.AddRange(Encoding.UTF8.GetBytes(endData));

        client.Headers.Add("Content-Type", contentType);

        client.UploadData("http://localhost:63247/Default.aspx", "POST", postByteArray.ToArray());
    }