1

when using PHP and sending POST requests, I do it like this:

 $ch = curl_init(POSTURL);
 curl_setopt($ch, CURLOPT_POST      ,1);
 curl_setopt($ch, CURLOPT_POSTFIELDS    , 'whatever');
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
 $Rec_Data = curl_exec($ch);

Now I want to use CURL in C#. I found "libcurl", but I don't quite know how to do it. I found some code snippets on the internet, but they don't really help.

So how could I translate that PHP code to C#?

Thanks a lot.

2
  • 1
    As @jacko says below, why aren't you using WebClient? What makes you need libcurl? Commented Mar 22, 2011 at 12:06
  • using libcurl is about 2.5 times faster than webclient or httpclient. Commented Mar 7, 2017 at 4:52

3 Answers 3

1

Here is a sample of how to use LibCurlNet

using System;
using SeasideResearch.LibCurlNet;

namespace Sample
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

            Easy easy = new Easy();
            Easy.WriteFunction wf = MyWriteFunction;
            easy.SetOpt(CURLoption.CURLOPT_URL, "http://google.com/index.html");
            easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
            easy.Perform();
            easy.Cleanup();
            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }

        private static int MyWriteFunction(byte[] buf, int size, int nmemb, Object extraData)
        {
            foreach (byte b in buf)
                Console.Write((char)b);

            return buf.Length;
        }
    }
}

For more examples just search stackoverflow for libcurl. There are plenty of code snippets roaming here under the [libcurl] tag. Hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, that seems to work fine. But there's a little problem: CURLOPT_RETURNTRANSFER doesn't exist. So I can send the request, but I don't get the "result" of the request :/
While I was looking at examples I noticed Easy.WriteFunction class that seems to be registering a callback function. I think that's what you need to get the result. Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData); easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
I got that too, but where is the result? I tried something like CURLcode cCode = easy.Perform();, but there's nothing interesting in cCode. Where else could the result be? (And thanks for your help, by the way)
Head scratching moment indeed! Guess I should download LibCurl and do a sample code :) -- Checking. Hope i can crack this in 30 minutes. Need to get back to office work after that.
Ok I've run the libcurl code above and I can see the response returned from google's webserver on console (through the callback function). If this method is not working, then please post your code implementation to see what is going on.
1

Here's a quick sample from CurlSharp

using System;
using CurlSharp;

internal class EasyGet
{
    public static void Main(String[] args)
    {
        Curl.GlobalInit(CurlInitFlag.All);

        try
        {
            using (var easy = new CurlEasy())
            {
                easy.Url = "http://www.google.com/";
                easy.WriteFunction = OnWriteData;
                easy.Perform();
            }
        }
        finally
        {
            Curl.GlobalCleanup();
        }   
    }

    public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
    {
        Console.Write(Encoding.UTF8.GetString(buf));
        return size*nmemb;
    }
}

CurlSharp is available for Win32 and Win64, as well as Linux and OSX (Mono).

Disclaimer: I am the author of the above mentioned library.

Comments

0

Why can't you use WebClient, WebRequest or HttpWebRequest? They are more than adequate for HTTP calls (including all the restful verbs).

4 Comments

I'd imagine its probably to do with the fact that he is coming from PHP world and feels more comfortable working with the component that he is used to :)
I understand, but conversely I wouldn't recommend someone moving to the PHP world using a managed .NET DLL when there are built-in PHP libraries to handle this. Learn the framework you're using.
Agreed, but that's another discussion. I was inclined to answer the same initially but that wouldn't have answered OP's question.
The correct answer is the correct answer, whether its what the OP was looking for or not... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.