Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert CURL to C#

Tags:

c#

http

post

curl

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);
        }
like image 619
Tommy Cawley Avatar asked Aug 14 '14 11:08

Tommy Cawley


People also ask

Can you use curl in C#?

You can also convert Curl requests to PHP, Python, JavaScript, and C # code. Click Run to execute the Convert Curl to HTTP Request online and see the results. The C#/. NET code was automatically generated for the Convert Curl HTTP Request example.

How do you turn curl into Postman?

Open POSTMAN. Click on "import" tab on the upper left side. Select the Raw Text option and paste your cURL command. Hit import and you will have the command in your Postman builder!

How do I convert curl to Python?

You can convert Curl commands to Python requests using the ReqBin Online Curl to Python convertor. The Curl to Python Converter uses the Python Requests Library for the generated code. Test your Curl commands online with our online Curl client and then convert Curl syntax to Python code with just one click.

What is curl command?

Client URL (cURL, pronounced “curl”) is a command line tool that enables data exchange between a device and a server through a terminal. Using this command line interface (CLI), a user specifies a server URL (the location where they want to send a request) and the data they want to send to that server URL.


2 Answers

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);
}
like image 111
crackalak Avatar answered Sep 20 '22 14:09

crackalak


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/

like image 43
olsh Avatar answered Sep 16 '22 14:09

olsh