cURL / Mailing Lists / curl-library / Single Mail

curl-library

Help needed to upload a file using libcurl.net

From: Uday Kumar Tirupathi <UdayKumar_T_at_infosys.com>
Date: Thu, 29 Jan 2015 05:45:33 +0000

Hi All,

I need to upload a file to a HTTPS server using libcurl.net.
I am getting " 403 - Forbidden: Access is denied, You do not have permission to view this directory or page using the credentials that you supplied" error.
Please find below the code that I am using to perform file upload

using System;
using System.IO;
using SeasideResearch.LibCurlNet;

class Upload
{
    public static void Main(String[] args)
    {
        try
        {
            CURLcode ret;
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
            FileStream fs = new FileStream("FILE NAME PATH", FileMode.Open,
                FileAccess.Read, FileShare.Read);

            Easy easy = new Easy();

            Easy.ReadFunction rf = new Easy.ReadFunction(OnReadData);
            easy.SetOpt(CURLoption.CURLOPT_READFUNCTION, rf);
            easy.SetOpt(CURLoption.CURLOPT_READDATA, fs);
            easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, false);
            easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false);
            easy.SetOpt(CURLoption.CURLOPT_HTTPAUTH, CURLhttpAuth.CURLAUTH_BASIC);

            Easy.DebugFunction df = new Easy.DebugFunction(OnDebug);
            easy.SetOpt(CURLoption.CURLOPT_DEBUGFUNCTION, df);
            easy.SetOpt(CURLoption.CURLOPT_VERBOSE, true);

            Easy.ProgressFunction pf = new Easy.ProgressFunction(OnProgress);
            easy.SetOpt(CURLoption.CURLOPT_PROGRESSFUNCTION, pf);

            easy.SetOpt(CURLoption.CURLOPT_URL, "HTTPS SERVER URL");
            easy.SetOpt(CURLoption.CURLOPT_USERPWD,"USER NAME" + ":" + "PASSWORD");

            easy.SetOpt(CURLoption.CURLOPT_FAILONERROR, false);
            easy.SetOpt(CURLoption.CURLOPT_UPLOAD, true);
            easy.SetOpt(CURLoption.CURLOPT_INFILESIZE, fs.Length);

            ret = easy.Perform();

            easy.Cleanup();

            fs.Close();

            Curl.GlobalCleanup();

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

    public static Int32 OnReadData(Byte[] buf, Int32 size, Int32 nmemb,
        Object extraData)
    {
        FileStream fs = (FileStream)extraData;
        return fs.Read(buf, 0, size * nmemb);
    }

    public static void OnDebug(CURLINFOTYPE infoType, String msg,
        Object extraData)
    {
        Console.WriteLine(msg);
    }

    public static Int32 OnProgress(Object extraData, Double dlTotal,
        Double dlNow, Double ulTotal, Double ulNow)
    {
        Console.WriteLine("Progress: {0} {1} {2} {3}",
            dlTotal, dlNow, ulTotal, ulNow);
        return 0; // standard return from PROGRESSFUNCTION
    }
}
Note: FILE NAME PATH, HTTPS SERVER URL, USER NAME, PASSWORD are set accordingly.

But I am able to successfully upload the same file through command line options given below

curl.exe -# -C - -i -k -u USER NAME:@ PASSWORD -F filedata=@"FILE NAME" HTTPS SEVER URL.

Could you please help me in resolving 403: Forbidden access error.

Regards,
Uday.

**************** CAUTION - Disclaimer *****************
This e-mail contains PRIVILEGED AND CONFIDENTIAL INFORMATION intended solely
for the use of the addressee(s). If you are not the intended recipient, please
notify the sender by e-mail and delete the original message. Further, you are not
to copy, disclose, or distribute this e-mail or its contents to any other person and
any such actions are unlawful. This e-mail may contain viruses. Infosys has taken
every reasonable precaution to minimize this risk, but is not liable for any damage
you may sustain as a result of any virus in this e-mail. You should carry out your
own virus checks before opening the e-mail or attachment. Infosys reserves the
right to monitor and review the content of all messages sent to or from this e-mail
address. Messages sent to or from this e-mail address may be stored on the
Infosys e-mail system.
***INFOSYS******** End of Disclaimer ********INFOSYS***

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2015-01-29