Archived Forum Post

Index of archived forum posts

Question:

C# bgResponseObject and HttpEndReceiveEventHandler. bgResultString where are you?

Oct 31 '14 at 05:16

I've search for samples, and in forums and documentation, but nothing mentions how to use Chilkat http asynchronously.

I've tried synchronously using QuickGet and confirm that my endpoint returns a response.

I debug the below code and confirm that a background Task is running and that it does notice there is an end and a http response 200. but none of my attempts to get the bgResultstring have been successful.

I set up HttpEndReceiveEventHandler, I've tried a horrible WHILE loop (sigh!) even.


simple version of code below:

class Program
{
    public static void Main(string[] args)
    {

    using (var chillkatHttpBgClient = new Chilkat.Http())
    {
        if (!chillkatHttpBgClient.IsUnlocked()) chillkatHttpBgClient.UnlockComponent(chillkatKey);
        chillkatHttpBgClient.UseBgThread = true;
        chillkatHttpBgClient.OnHttpEndReceive += new Chilkat.Http.HttpEndReceiveEventHandler(HttpEndReceiveEventHandlerEvent);

        var bgHttpResponseByteArray = chillkatHttpBgClient.QuickGet(requestUri);
        Console.WriteLine("Press enter to continue.");
        Console.ReadLine();
        chillkatHttpBgClient.OnHttpEndReceive -= new Chilkat.Http.HttpEndReceiveEventHandler(HttpEndReceiveEventHandlerEvent);
    }
}
public void HttpEndReceiveEventHandlerEvent(object response, Chilkat.StatusEventArgs status)
{
    Console.WriteLine("response: " + response.ToString());
    string httpResponseString = (string)response;
    Console.WriteLine("return status success: " + status.Success.ToString());
    Console.WriteLine("return value: " + httpResponseString);
}

class Program
{
public static void Main(string[] args)
{
    //content removed ....
    using (var chillkatHttpBgClient = new Chilkat.Http())
        {
            //for longer calls
            if (!chillkatHttpBgClient.IsUnlocked()) chillkatHttpBgClient.UnlockComponent(chillkatKey);
            chillkatHttpBgClient.KeepEventLog = true;
            chillkatHttpBgClient.VerboseLogging = true;
            chillkatHttpBgClient.ClearBgEventLog();

            chillkatHttpBgClient.UseBgThread = true;
            chillkatHttpBgClient.AllowGzip = true;
            chillkatHttpBgClient.Accept = "text/xml";

            if (!chillkatHttpBgClient.SetSslClientCertPfx(pfxFilename: pfxFilePath, pfxPassword: pfxPassword))
                throw new Exception("pfxError:" + chillkatHttpBgClient.LastErrorText);

            chillkatHttpBgClient.OnHttpEndReceive += new Chilkat.Http.HttpEndReceiveEventHandler(HttpEndReceiveEventHandlerEvent);

            var bgHttpResponseByteArray = chillkatHttpBgClient.QuickGet(requestUri);ient.LastErrorText);

            while(chillkatHttpBgClient.BgTaskRunning)
            {
                Console.WriteLine("##############################");
                int logCount = chillkatHttpBgClient.EventLogCount;
                if (logCount > 0)
                {
                    for (int logName = 0; logName < logCount; logName++)
                    {
                        Console.WriteLine("Log:{0}",logName.ToString());
                        //Console.WriteLine("{0}:{1}",
                        //  chillkatHttpBgClient.EventLogName(logName),
                        //  chillkatHttpBgClient.EventLogValue(logName));
                    }
                }
                chillkatHttpBgClient.ClearBgEventLog();
                Console.WriteLine("-");
            }

            Console.WriteLine("LastStatus: " + chillkatHttpBgClient.LastStatus);
            Console.WriteLine("LastResponseHeader: " + chillkatHttpBgClient.LastResponseHeader);
            Console.WriteLine("ResponseTxt: " + chillkatHttpBgClient.BgResultString);

            using (Chilkat.HttpResponse bankStreamResponse1 = chillkatHttpBgClient.BgResponseObject())
            {
                if (bankStreamResponse1 == null)
                    Console.WriteLine("BgResponseObject empty");
                else
                    Console.WriteLine("bankStreamResponse1 value: " + bankStreamResponse1.BodyStr);
            }
            Console.WriteLine("Press enter to continue.");
            Console.ReadLine();
            chillkatHttpBgClient.OnHttpEndReceive -= new Chilkat.Http.HttpEndReceiveEventHandler(HttpEndReceiveEventHandlerEvent);
            Console.WriteLine("OnHttpEndReceive unsubscribed");
        }

        using (var httpReq = new Chilkat.HttpRequest())
        {
            httpReq.AddHeader("Host", "www.example.com");
            httpReq.SetFromUrl("");
        }
    }

    public void HttpEndReceiveEventHandlerEvent(object response, Chilkat.StatusEventArgs status)
    {
        Console.WriteLine("response: " + response.ToString());
        string httpResponseString = (string)response;
        Console.WriteLine("return status success: " + status.Success.ToString());
        Console.WriteLine("return status: " + status.ToString());
        Console.WriteLine("return value: " + httpResponseString);
    }
       }