2

私は .NET C# を使用HTTPrequestしており、Curl で記述されているのと同じ方法で POST を作成するために使用したいと考えています。

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

私は得てError 500 Internal error from the Internet serverいます。

送信するデータはCSVファイルです。

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();

私は何を間違っていますか?

4

2 に答える 2

0

cURL は multipart/form-data コンテンツ タイプを使用してリクエストを作成します。テキストパラメーターとファイルの両方を渡しているので、これはあなたの場合に必要だと思います。残念ながら、.Net フレームワークには、このコンテンツ タイプを作成するためのサポートが組み込まれていないようです。

ここで自分でそれを行う方法のサンプルコードを提供する SO に関する質問があります: C# クライアントからのマルチパートフォーム

参考までに、その cURL コマンドによって生成される HTTP リクエストは次のようになります。

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--
于 2014-11-16T09:56:10.953 に答える
-1

フォーム投稿用にデータを URL エンコードする必要はありませんか? http://en.wikipedia.org/wiki/POST_%28HTTP%29#Use_for_submitting_web_forms

于 2012-12-03T14:29:35.197 に答える