2

I´m using .NET C# and want to use HTTPrequest to make a POST in the same way as it is written in Curl:

curl
--header ’Accept: text/html’
--user ’test1:password’
--Form ’wait=0’
--Form ’data=@data.csv;type=text/csv’
some URL address here

I´m getting Error 500 Internal error from the Internet server.

The data to be send is a CSV file.

My source code in C#:

string data = "Time, A, B\n20120101, 100, 24\n20120102\n, 101, 27";

// Create a request using a URL that can receive a post.
                    request = WebRequest.Create(url);
                    //Set authorization
                    request.Credentials = new NetworkCredential(username, password);
                    // Set the Method property of the request to POST.
                    request.Method = "POST";

                    // Create POST data and convert it to a byte array.
                    byte[] byteArray = Encoding.UTF8.GetBytes(data);
                    // Set the ContentType property of the WebRequest.
                    request.ContentType = "application/x-www-form-urlencoded";
                    // Set the ContentLength property of the WebRequest.
                    request.ContentLength = byteArray.Length;
                    // Get the request stream.
                    dataStream = request.GetRequestStream();
                    // Write the data to the request stream.
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    // Close the Stream object.
                    dataStream.Close();

// Get the original response.
                WebResponse response = request.GetResponse();
                this.Status = ((HttpWebResponse)response).StatusDescription;
                // Get the stream containing all content returned by the requested server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content fully up to the end.
                string responseFromServer = reader.ReadToEnd(); 
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();

What am I doing wrong?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • When the content type is set to `application/x-www-form-urlencoded` then it would generally be formatted the same as a `GET` request would be. The other option is `multipart/form-data` which I am less sure about. The best way of course is to look at the request that both generate to see what is different between them... – Chris Dec 03 '12 at 14:36
  • Dont know if your request is ok or not, but a 500 code means that the server run into an error, not something that should happen as a result of a bad request. – user1852503 Nov 15 '14 at 16:44
  • If you look on the exception that is thrown you will see a response stream on it. Take a look at the contents, the server may have given you some more helpful error details. – Martin Brown Nov 15 '14 at 17:10
  • The curl command creates a multipart/form-data request. – Martin Brown Nov 16 '14 at 09:14

2 Answers2

0

cURL uses the multipart/form-data content type to create its requests. I think this is required in your case as you are passing both a text parameter and a file. Sadly the .Net framework does not appear to have built in support for creating this content type.

There is a question on SO that gives example code for how to do it yourself here: Multipart forms from C# client

For reference the HTTP request generated by that cURL command looks something like this:

POST http://www.example.com/example HTTP/1.1
Authorization: Basic J3Rlc3Q6cGFzc3dvcmQn
User-Agent: curl/7.33.0
Host: www.example.com
Accept: text/html
Connection: Keep-Alive
Content-Length: 335
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------99aa46d554951aa6

--------------------------99aa46d554951aa6
Content-Disposition: form-data; name="'wait"

0'
--------------------------99aa46d554951aa6
Content-Disposition: form-data; name="'data"; filename="data.csv"
Content-Type: text/csv'

Time, A, B\n20120101, 100, 24\n20120102\n, 101, 27 

--------------------------99aa46d554951aa6--
Community
  • 1
  • 1
Martin Brown
  • 24,692
  • 14
  • 77
  • 122
-1

Doesn't data need to be URL encoded for a form post? http://en.wikipedia.org/wiki/POST_%28HTTP%29#Use_for_submitting_web_forms

C.M.
  • 1,474
  • 13
  • 16