I need help with sending a file to a remote server using a POST request (along with other either POST and/or GET data).
I have researched this an the only thing that does both (GET and POST at the same time) I have found is cURL.
I have used cURL in the past with PHP and it has been very easy to implement. With .NET (using cURL .NET library http://curl.haxx.se/libcurl/dotnet/
) however I'm running into an issue where I am unable to "attach" the file to the HTTP POST request. If there are alternate methods for sending data (including a file) to a remote server (where I have a receiving scipt expecting a POST request) without using cURL please let me know.
Below I have a working code that takes a variable named "fields" containing a series of key=value pairs connected with ampersand (same as URI query string): key1=value1&key2=value2&..
The problem I am running into here is how to get a file across using this method in .NET. Again if there are other (better?) ways of accomplishing this please do let me know.
On the other end (the remote server) I have a script that processes the received date (as if it was submitted using a standard HTML form via HTTP POST) and the additional variables (submitted as the HTTP GET variables).
If I just use a file name for the CURLOPT_POSTFIELDS option as below I only get the file name as the variable - no file though:
If I add the contents as in the script below, I see the variable but no file or file name. So seems like I am missing on how the file stream needs to be formatted or something.
On the receiving end the script generates also a log file with all headers and data it receives along with attempting to receive the submitted file for confirmation purposes.
Any help would be greatly appreciated!
// This is in C#.NET - which works except for the file transfer part
string url = "https://myreceivingservice.com/requestResponse.php";
string fileLocation = "C:\\myUploadFolder\\myFile.txt";
string fields = "?key1=value1&key2=value2";
System.IO.StreamReader sr = new System.IO.StreamReader(fileLocation);
fileContents = sr.ReadToEnd();
CURLcode context = Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
Easy easy = new Easy();
easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, "myFile=" + fileContents);
easy.SetOpt(CURLoption.CURLOPT_URL, url + "?" + fields);
// This is how I have used it in PHP - which works including the file transfer part
$url = "https://myreceivingservice.com/requestResponse.php";
$fileLocation = "C:\myUploadFolder\myFile.txt";
$fields = "?key1=value1&key2=value2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("myFile"=>"@$fileLocation");
curl_setopt($ch, CURLOPT_URL, $url . $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$postResult = curl_exec($ch);