0

I need to make a curl call in c# below is the curl -s -S -F "pv=6" -F "acct=test" -F "app=applyload" -F "fmt=tab_delimited" -F "data=@sample.tsv"

  public string CreateLoadnew()
    {

           NameValueCollection nvc = new NameValueCollection();
        nvc.Add("pv", "6");
        nvc.Add("acct", "test");
        nvc.Add("app", "applyload");
        nvc.Add("fmt", "tab_delimited");
        string[] files = new string[] { @"C:\Files\sample.tsv" };
        return UploadFilesToRemoteUrl("https://test.aljex.com/api.php", files, nvc);

    }
    public static string UploadFilesToRemoteUrl(string url, string[] files, NameValueCollection formFields = null)
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
        string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ContentType = "multipart/form-data; boundary=" +
                                boundary;
        request.Method = "POST";
        request.KeepAlive = true;
        request.UserAgent = "curl/7.33.0";
        Stream memStream = new System.IO.MemoryStream();

        var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
                                                                boundary + "\r\n");
        var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
                                                                    boundary + "--");


        string formdataTemplate = "\r\n--" + boundary +
                                    "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

        if (formFields != null)
        {
            foreach (string key in formFields.Keys)
            {
                string formitem = string.Format(formdataTemplate, key, formFields[key]);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                memStream.Write(formitembytes, 0, formitembytes.Length);
            }
        }

        string headerTemplate =
            "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
            "Content-Type: text/tab-separated-values\r\n\r\n";

        for (int i = 0; i < files.Length; i++)
        {
            memStream.Write(boundarybytes, 0, boundarybytes.Length);
            var header = string.Format(headerTemplate, "uplTheFile", files[i]);
            var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

            memStream.Write(headerbytes, 0, headerbytes.Length);

            using (var fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
            {
                var buffer = new byte[1024];
                var bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    memStream.Write(buffer, 0, bytesRead);
                }
            }
        }

        memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
        request.ContentLength = memStream.Length;

        using (Stream requestStream = request.GetRequestStream())
        {
            memStream.Position = 0;
            byte[] tempBuffer = new byte[memStream.Length];
            memStream.Read(tempBuffer, 0, tempBuffer.Length);
            memStream.Close();
            requestStream.Write(tempBuffer, 0, tempBuffer.Length);
        }

        using (var response = request.GetResponse())
        {
            Stream stream2 = response.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2);
            return reader2.ReadToEnd();
        }
    }

Response if fiddler not used is always returns me an empty string no error

on using the fiddler I get HTTP/1.1 200 Connection Established FiddlerGateway: Direct StartTime: 17:39:18.828 Connection: close

fiddler.network.https> HTTPS handshake to test.aljex.com (for #109) failed. System.IO.IOException Received an unexpected EOF or 0 bytes from the transport stream.

visual studio error https://www.screencast.com/t/BJgklp4wX

Any suggestions of what i am doing wrong

Regards

Altaf
  • 1
  • 2
    Your code would be a LOT simpler if you used HttpClient instead of raw HttpWebRequest. Besides, there's no `curl call`. You are asking how to POST form values, since the `-s -S` parameters don't affect how the calls are made. If you check [this almost duplicate question](https://stackoverflow.com/questions/20005355/how-to-post-data-using-httpclient) you'll see that you only need 4-5 lines – Panagiotis Kanavos Dec 15 '17 at 12:28
  • 1
    PS why are you *downgrading* security by specifying SSL3 ? – Panagiotis Kanavos Dec 15 '17 at 12:28
  • Hello Thank you for the help Was successfully able to make the call and got the desired output replaced the httpWebRequest by httpclient and also removed the ssl3 which was unnecessary – Altaf Dec 22 '17 at 06:59

0 Answers0