Convert CURL to C#

30,551

Solution 1

I modified the first one to write data to the request stream as per http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx, does this work:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseurl);
request.Method = "POST";
request.Accept = "application/json";
request.Credentials = new NetworkCredential(username, password);
request.UserAgent = "curl/7.37.0";
request.ContentType = "application/x-www-form-urlencoded";

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    string data = "browser=Win7x64-C1|Chrome32|1024x768&url=http://www.google.com";

    streamWriter.Write(data);
}

var response = request.GetResponse();
string text;

using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
    values.Add(text);
}

Solution 2

Just implemented an experimental ASP.NET Core app that turns curl commands into C# code using Roslyn

Give it a try, please:
https://curl.olsh.me/

Share:
30,551
Tommy Cawley
Author by

Tommy Cawley

Updated on August 29, 2020

Comments

  • Tommy Cawley
    Tommy Cawley over 3 years

    I have spent ages trying various different ways to convert this curl to c#. Could someone please help. I am trying to do a http post and keep getting error 500. here is what I want to convert:

    curl --user username:password -X POST -d "browser=Win7x64-C1|Chrome32|1024x768&url=http://www.google.com" http://crossbrowsertesting.com/api/v3/livetests/
    

    and this is what I have so far:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseurl);
    request.Method = "POST";
    request.Accept = "application/json";
    request.Credentials = new NetworkCredential(username, password);
    
    var response = request.GetResponse();
    string text;
    
    using (var sr = new StreamReader(response.GetResponseStream()))
    {
        text = sr.ReadToEnd();
        values.Add(text);
    }
    

    Tried this method too but it didn't work:

    List<string> data = new List<string>();
            data.Add("browser=Win7x64-C1|Chrome20|1024x768");
            data.Add("url=URL");
            data.Add("format=json");
            data.Add("callback=doit");
            var request = WebRequest.Create("CrossBrowserTestingURL");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Credentials = new NetworkCredential(username, password);
            using (var writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write("data=" + data);
            }
    
            var response = request.GetResponse();
    
            string text;
    
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                text = sr.ReadToEnd();
                values.Add(text);
            }
    
  • Tommy Cawley
    Tommy Cawley over 9 years
    unfortunately that did not work. Still getting error 500 from the server. The curl works but the c# method doesn't. Any ideas?
  • crackalak
    crackalak over 9 years
    I've changed the answer to use JSON as per the API docs, can you try again?
  • Tommy Cawley
    Tommy Cawley over 9 years
    still returning error 500 internal server error unfortunately :(
  • crackalak
    crackalak over 9 years
    I created an account to debug, the request data does have to be in form format and the UserAgent needs to be set. I used the cURL user agent for sake of testing. I guess the API is still in beta after all!
  • Rudy Hinojosa
    Rudy Hinojosa about 9 years
    You will need to add a NameValueCollection right after your request.ContentType line. Like the following: <br /> NameValueCollection collection = new NameValueCollection() <br /> collection.Add("username",value); <br /> collection.Add("password", value); <br /> request.Headers.Add(collection);