如何在C#中使用Web请求发送.CSV文件



我想在 POST请求中发送.csv文件,其中内容类型为" multipart/form-data",在身体中,有一些键和.csv文件以下是我尝试过的代码:

string url = "*****URL*****";
string root = AppDomain.CurrentDomain.BaseDirectory;
string file = root + "Folder\file.csv";
FileInfo fileInfo = new FileInfo(file);
int fileLength = (int)fileInfo.Length;
FileStream rdr = new FileStream(file, FileMode.Open);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);    
request.Method = "POST";
request.ContentType = "multipart/form-data";
request.Headers["Content-Type"] = "multipart/form-data";
int bytesRead = 0;
byte[] requestByte = new byte[fileLength];
request.ContentLength = requestByte.Length;
using (Stream requestStream = request.GetRequestStream())
{
    while ((bytesRead = rdr.Read(requestByte, 0, requestByte.Length)) != 0)
    {
         requestStream.Write(requestByte, 0, bytesRead);
         requestStream.Close();
    }
}
string responseData;
using (StreamReader responseStream = new StreamReader(request.GetResponse().GetResponseStream()))
{
    responseData = responseStream.ReadToEnd();
    responseStream.Close();
}

但这在内容类型中给出了错误,我可能做错了。请在这里帮助我。

最后,此代码对我有用,

string boundary = "---" + DateTime.Now.Ticks.ToString("x");
var client = new RestClient("*****URL*****");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "multipart/form-data; boundary=" + boundary);
request.AddParameter("multipart/form-data; boundary=" + boundary, "--"+ boundary + "rnContent-Disposition: form-data; name="**KEY**"; filename="***File Name***.csv"rnContent-Type: text/csvrnrnrn--" + boundary + "--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

这是一种更好的方法:

main()
{
    HttpWebRequest request;
    HttpWebResponse response = null;
    string url = "*****URL*****";
    string path = @"..test.csv";
    Random rand = new Random();
    string contentType = "text/csv";
    byte[] header = RequestHeader(boundary,path,nameValue, contentType);
    request = (HttpWebRequest)WebRequest.Create(url);
    RequestParameters(header, path, boundary);
    response = (HttpWebResponse)request.GetResponse();
}
private byte[] RequestHeader(string boundary, string path, string nameValue, string contentType)
{
    return System.Text.Encoding.ASCII.GetBytes("rn--" + boundary + "rnContent-Disposition: form-data; name="" + nameValue + ""; filename="" + System.IO.Path.GetFileName(path) + ""rnContent-Type:" + contentType + "rnrn");
}
private void RequestParameters(byte[] header, string path, string boundary)
{
    byte[] trailer = System.Text.Encoding.ASCII.GetBytes("rn--" + boundary + "--rn");
    request.Method = "post";
    request.KeepAlive = true;
    request.ContentType = "multipart/form-data" + boundary;
    data_stream = request.GetRequestStream();
    data_stream.Write(header, 0, header.Length);
    byte[] file_bytes = System.IO.File.ReadAllBytes(path);
    data_stream.Write(file_bytes, 0, file_bytes.Length);
    data_stream.Write(trailer, 0, trailer.Length);
    data_stream.Close();
}

我写了一个使用webclient进行此操作的多部分助手。

相关内容

  • 没有找到相关文章

最新更新