Razvan V

Razvan V

  • NA
  • 4
  • 5.6k

Converting PHP cURL to C# WebClient

Feb 23 2019 8:29 AM
I am trying to convert a piece of PHP cURL code to C# using the WebClient but I can't figure out how to implement the CurlFile parameter from the array.
 
Here is the original PHP code:
  1. $post_data = array('password'=>'pswd',  
  2. 'username'=>'usr',  
  3. 'textfile'=>new CURLFile('C:\data_stats.txt'),  
  4. );  
  5.   
  6. $curl_obj = curl_init('https://www.website.com');  
  7. curl_setopt($curl_obj, CURLOPT_POST, true);  
  8. curl_setopt($curl_obj, CURLOPT_POSTFIELDS, $post_data);  
  9. $data = curl_exec($curl_obj);  
And there is the C# code I came up with (I managed to send the password and username parameters but I don't know how to send the file with the key 'textfile' along with those parameters)
  1. using (var client = new WebClient())  
  2. {  
  3. var post_data = new NameValueCollection();  
  4. post_data["password"] = "pswd";  
  5. post_data["username"] = "usr";  
  6.   
  7. var response = client.UploadValues("www.website.com", post_data);  
  8.   
  9. }  

Answers (1)