I am trying to curl file contents to an external source. I can curl it correctly from the command line, but I am having problem sending the string in the -d switch in code. Here is the gist of the curl command
curl.exe -u userName:password -H "Content-Type: text/plain" -X PUT https://someIp/command?filename=filename.txt -d "content of filename.text is here" --insecure I can send the file, and they receive it on the other end, the problem is that the content of the file isn't making it over to them. Does anyone have any experience or ideas here? Here is the code from my proof of concept.
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback ( delegate { return true; } ); // create the requst address to send the file to string requestAddress = string.Format("{0}{1}", this.CurlAddress, Path.GetFileName(fileName)); // spin up the request object, set neccessary paramaters var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(requestAddress); request.ContentType = "text/plain"; request.Method = "PUT"; request.Credentials = new NetworkCredential("userName", "password"); // open the web request stream using (var stream = request.GetRequestStream()) { // create a writer to the request stream using (var writer = new StringWriter()) { // write the text to the stream writer.Write(File.ReadAllLines(fileName)); stream.Close(); } } // Get the response HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream, Encoding.UTF8); String responseString = reader.ReadToEnd(); Console.WriteLine(string.Format("Response: {0}", responseString)); }
HttpClientwithPutAsyncandStringContentmight be what you're after.StringWriterwriterwith the request stream?