cURL到带有参数的C#HttpWebRequest

时间:2018-08-09 20:23:45

标签: c# curl post httpwebrequest

我正在尝试使用HttpWebRequest将以下cURL语句转换为pot请求。

curl -F datafile=@C:\Path\Students.csv -F account=accountName -F username=un -F key=acctkey -F type=1 -F format=CSV -F date=8 -F header=No -F email=email@school.org -F description=student_data_update https://webaddress.com/district_import.php

我在此网站和其他网站上尝试了许多不同的建议。这似乎是最接近正确的,但是我没有到达那里。我正在为cURL参数创建一个postString。 postString是根据发送给函数的参数构建的,但这就是它的样子。

    string postString = "account=accountName&type=1&format=CSV&date=8&header=No&description=student_data_update&username=un&key=acctkey&email=email@school.org";

我的代码如下:

            HttpWebRequest requestToServerEndpoint =
            (HttpWebRequest)WebRequest.Create(new Uri(url + @"/"));


            string boundaryString = "----SomeRandomText";

            // Set the http request header \\
            requestToServerEndpoint.Method = WebRequestMethods.Http.Post;
            requestToServerEndpoint.ContentType = "multipart/form-data; boundary=" + boundaryString;
            requestToServerEndpoint.KeepAlive = true;
            requestToServerEndpoint.Credentials = new System.Net.NetworkCredential(un, p);
            requestToServerEndpoint.Headers.Add("account", "acctName");

            // Use a MemoryStream to form the post data request,
            // so that we can get the content-length attribute.
            MemoryStream postDataStream = new MemoryStream();
            StreamWriter postDataWriter = new StreamWriter(postDataStream);

            // Include value from the myFileDescription text area in the post data
            postDataWriter.Write("\r\n--" + boundaryString + "\r\n");
            postDataWriter.Write("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}",
            "myFileDescription",
            "A sample file description");

            // Include the file in the post data
            postDataWriter.Write("\r\n--" + boundaryString + "\r\n");
            postDataWriter.Write("Content-Disposition: form-data;"
            + "name=\"{0}\";"
            + "filename=\"{1}\""
            + "\r\nContent-Type: {2}\r\n\r\n",
            "myFile",
            Path.GetFileName(file),
            Path.GetExtension(file));
            postDataWriter.Flush();
            // Read the file
            FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            byte[] bytes = Encoding.ASCII.GetBytes(postString);
            postDataStream.Write(bytes, 0, bytes.Length);
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                postDataStream.Write(buffer, 0, bytesRead);
            }
            fileStream.Close();

            postDataWriter.Write("\r\n--" + boundaryString + "--\r\n");
            postDataWriter.Flush();

            // Set the http request body content length
            requestToServerEndpoint.ContentLength = postDataStream.Length;

            // Dump the post data from the memory stream to the request stream
            using (Stream s = requestToServerEndpoint.GetRequestStream())
            {
                postDataStream.WriteTo(s);
            }
            StreamReader responseReader = new StreamReader(requestToServerEndpoint.GetResponse().GetResponseStream());
            string responseData = responseReader.ReadToEnd();
            postDataStream.Close();
            responseReader.Close();
            requestToServerEndpoint.GetResponse().Close();

从响应中检查错误时。我的错误是:帐户丢失。 我已经添加了标题和postString中的第一个参数。我想念什么?

1 个答案:

答案 0 :(得分:0)

如果将Web服务器设置为匿名获取文件,则类似的事情应该可以解决。大多数服务器不会。因此,准备转移到生产环境时,准备向服务器添加凭据以对WebClient进行身份验证。

if noun2[:1] in 'aeiou':