1

I am trying to use libcurl.net to connect to a server and return the json from it. But instead of returning the JSON, it returns "CURLE_OK". Heres my code:

Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_WIN32);
Easy easy = new Easy();
var agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)";
easy.SetOpt(CURLoption.CURLOPT_URL, string.Format("http://vikinglogue.com/api/get_posts/?page={0}", currentPage));
easy.SetOpt(CURLoption.CURLOPT_HTTPGET, 1);
easy.SetOpt(CURLoption.CURLOPT_USERAGENT, agent);
easy.SetOpt(CURLoption.CURLOPT_TRANSFERTEXT, 1);
var responce = easy.Perform();
MessageBox.Show(responce.ToString());
easy.Cleanup();

Anyone have any idea what I'm doing wrong here? Please don't suggest an answer using WebClient, because, I can't use it (not by choice). Thanks.

1 Answer 1

1

You're probably expecting CURLOPT_TRANSFERTEXT to act like PHP's CURLOPT_RETURNTRANSFER. It doesn't, as the former is documented as "tells the library to use ASCII mode for ftp transfers", which is totally irrelevant in this case as it's an HTTP request.

See the EasyGet sample, you need to pass a callback to a write function:

public static void Main(String[] args)
{
    try {
        Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

        Easy easy = new Easy();
        Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);

        easy.SetOpt(CURLoption.CURLOPT_URL, args[0]);
        easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
        easy.Perform();
        easy.Cleanup();

        Curl.GlobalCleanup();
    }
    catch(Exception ex) {
        Console.WriteLine(ex);
    }
}

public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb,
    Object extraData)
{
    Console.Write(System.Text.Encoding.UTF8.GetString(buf));
    return size * nmemb;
}
Sign up to request clarification or add additional context in comments.

Comments

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.