12

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
  • 185
  • 2
  • 3
  • 12
  • Don't forget to write the data into request stream.. – L.B Aug 14 '14 at 11:32
  • could you please post a sample of how to do so. This is new to me. Thanks – Tommy Cawley Aug 14 '14 at 13:12
  • Is this a duplicate of [.net - Making a cURL call in C#](http://stackoverflow.com/questions/7929013/making-a-curl-call-in-c-sharp) or [c# - WebRequest Equivalent to CURL command](http://stackoverflow.com/questions/21255725/webrequest-equivalent-to-curl-command#)? – Sam Hobbs Jan 03 '17 at 06:18

3 Answers3

19

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);
}
crackalak
  • 563
  • 3
  • 7
  • unfortunately that did not work. Still getting error 500 from the server. The curl works but the c# method doesn't. Any ideas? – Tommy Cawley Aug 18 '14 at 08:31
  • I've changed the answer to use JSON as per the API docs, can you try again? – crackalak Aug 18 '14 at 10:30
  • still returning error 500 internal server error unfortunately :( – Tommy Cawley Aug 18 '14 at 15:07
  • 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! – crackalak Aug 18 '14 at 19:15
  • You will need to add a NameValueCollection right after your request.ContentType line. Like the following:
    NameValueCollection collection = new NameValueCollection()
    collection.Add("username",value);
    collection.Add("password", value);
    request.Headers.Add(collection);
    – Rudy Hinojosa Apr 02 '15 at 21:52
13

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/

olsh
  • 464
  • 1
  • 6
  • 14
  • This website sends your curl commands to its server to do the conversion. You may want to remove sensitive data from your command (such as cookies and OAuth tokens) before using it. – Boris Verkhovskiy Sep 20 '22 at 05:26
0

You can paste your command into curlconverter.com/csharp/ and it will convert it into this code using HttpClient:

using System.Net.Http.Headers;

HttpClient client = new HttpClient();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://crossbrowsertesting.com/api/v3/livetests/");

request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("username:password")));

request.Content = new StringContent("browser=Win7x64-C1|Chrome32|1024x768&url=http://www.google.com");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103