PHP curl to .NET HttpRequest:文件上传到服务器

我想要实现的是使用token身份validation(网络凭据中不需要用户名或密码)将单个/多个.csv文件上传到服务器,并且还会收到响应。 我有一个使用curl libs编写的PHP脚本(我的知识非常少),这个脚本实现了目的,但我打算将它转换为c#(HttpRequest)。

PHP脚本:

 $Files,'token'=>'0fc0975128d45cac2cechjhj676t5ft76f')); // Execute the handle $execResult=curl_exec($ch); if($execResult=== false) { echo 'Curl error: ' . curl_error($ch); } else { echo 'Operation completed without any errors'; echo $execResult; } } function redirect($redirect,$redirect_2,$message) ///confirm box pop up { echo "javascript: var ask = confirm('".$message."'); if(ask==true) { } else { window.location = '".$redirect_2."'; } "; } function curl_setopt_custom_postfields($ch, $postfields, $headers = null) { $algos = hash_algos(); $hashAlgo = null; foreach ( array('sha1', 'md5') as $preferred ) { if ( in_array($preferred, $algos) ) { $hashAlgo = $preferred; break; } } if ( $hashAlgo === null ) { list($hashAlgo) = $algos; } $boundary = '----------------------------' . substr(hash($hashAlgo, 'cURL-php-multiple-value-same-key-support' . microtime()), 0, 12); $body = array(); $crlf = "\r\n"; $fields = array(); foreach ( $postfields as $key => $value ) { if ( is_array($value) ) { foreach ( $value as $v ) { $fields[] = array($key, $v); } } else { $fields[] = array($key, $value); } } foreach ( $fields as $field ) { list($key, $value) = $field; if ( strpos($value, '@') === 0 ) { preg_match('/^@(.*?)$/', $value, $matches); list($dummy, $filename) = $matches; $body[] = '--' . $boundary; $body[] = 'Content-Disposition: form-data; name="' . $key . '"; filename="' . basename($filename) . '"'; $body[] = 'Content-Type: application/octet-stream'; $body[] = ''; $body[] = file_get_contents($filename); } else { $body[] = '--' . $boundary; $body[] = 'Content-Disposition: form-data; name="' . $key . '"'; $body[] = ''; $body[] = $value; } } $body[] = '--' . $boundary . '--'; $body[] = ''; $contentType = 'multipart/form-data; boundary=' . $boundary; $content = join($crlf, $body); $contentLength = strlen($content); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Length: ' . $contentLength, 'Expect: 100-continue', 'Content-Type: ' . $contentType, )); curl_setopt($ch, CURLOPT_POSTFIELDS, $content); } ?> 

在这里阅读: http : //www.c-sharpcorner.com/article/using-webrequest-and-webresponse-classes/并使用Postman我在c#中提出了这个等效的请求:

 var client = new RestClient("http://demo.schooling.net/school/attendance"); var request = new RestRequest(Method.POST); request.AddHeader("postman-token", "0fc0975128d45cac2cechjhj676t5ft76f"); request.AddHeader("cache-control", "no-cache"); request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"); request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", " ------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\nfe60313b0edfdfaf757f974481659688\r\n ------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Files[0]\"; filename=\"democollege-1-1-17.csv\"\r\nContent-Type: text/csv\r\n\r\n\r\n ------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Files[1]\"; filename=\"democollege-1-1-17.csv\"\r\nContent-Type: text/csv\r\n\r\n\r\n ------WebKitFormBoundary7MA4YWxkTrZu0gW-- ", ParameterType.RequestBody); IRestResponse response = client.Execute(request); 

这是将任何.csv文件发送到url并使用C#从服务器获得正确响应的正确方法吗?

更新:我尝试使用WebClient方法,但它抛出了exception:

 Too many automatic redirections were attempted. 

我该如何解决? 我正在发送令牌吗? 它在上面的PHP脚本中的显示方式?

 [HttpPost] public void SendCSV() { WebClient myWebClient = new WebClient(); var authHeader = "token=0fc0975128d45cac2cechjhj676t5ft76f"; myWebClient.Headers.Add("Authorization", authHeader); string fileName = "E:\\Uploads\\demo-1-2-3.csv"; string uriString = "http://demo.schooling.net/school/attendance"; byte[] responseArray = myWebClient.UploadFile(uriString, fileName); } 

最好的祝福。