C
C
Explaining the Rest ClearResponseBodyStream Method
See more Azure Cloud Storage Examples
The ClearResponseBodyStream method would be needed if your applicaiton called SetResponseBodyStream to send the response to a stream for one request, but then wants to subsequently do something else. The application must call ClearResponseBodyStream to no longer send responses to the stream.Chilkat C Downloads
#include <C_CkRest.h>
#include <C_CkStream.h>
#include <C_CkBinData.h>
void ChilkatSample(void)
{
BOOL success;
HCkRest rest;
BOOL bTls;
int port;
BOOL bAutoReconnect;
HCkStream fileStream;
int expectedStatus;
const char *responseStr;
HCkBinData bd;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rest = CkRest_Create();
// Connect to the web server
bTls = TRUE;
port = 443;
bAutoReconnect = TRUE;
success = CkRest_Connect(rest,"www.chilkatsoft.com",port,bTls,bAutoReconnect);
if (success != TRUE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
return;
}
// Setup a file stream for the download
fileStream = CkStream_Create();
CkStream_putSinkFile(fileStream,"qa_output/starfish.jpg");
// Indicate that the call to FullRequestNoBody should send the response body
// to fileStream if the response status code is 200.
// If a non-success response status code is received, then nothing
// is streamed to the output file and the error response is returned by FullRequestNoBody.
expectedStatus = 200;
CkRest_SetResponseBodyStream(rest,expectedStatus,TRUE,fileStream);
responseStr = CkRest_fullRequestNoBody(rest,"GET","/images/starfish.jpg");
if (CkRest_getLastMethodSuccess(rest) == FALSE) {
// Examine the request/response to see what happened.
printf("response status code = %d\n",CkRest_getResponseStatusCode(rest));
printf("response status text = %s\n",CkRest_responseStatusText(rest));
printf("response header: %s\n",CkRest_responseHeader(rest));
printf("response body (if any): %s\n",responseStr);
printf("---\n");
printf("LastRequestStartLine: %s\n",CkRest_lastRequestStartLine(rest));
printf("LastRequestHeader: %s\n",CkRest_lastRequestHeader(rest));
CkRest_Dispose(rest);
CkStream_Dispose(fileStream);
return;
}
printf("downloaded starfish.jpg\n");
// Clear the response body stream previously set in the call to SetResponseBodyStream.
CkRest_ClearResponseBodyStream(rest);
// Download something else, but this time send the response body to bd.
bd = CkBinData_Create();
success = CkRest_FullRequestNoBodyBd(rest,"GET","/images/penguins.jpg",bd);
if (success == FALSE) {
// Examine the request/response to see what happened.
printf("response status code = %d\n",CkRest_getResponseStatusCode(rest));
printf("response status text = %s\n",CkRest_responseStatusText(rest));
printf("response header: %s\n",CkRest_responseHeader(rest));
printf("response body (if any): %s\n",CkBinData_getString(bd,"utf-8"));
printf("---\n");
printf("LastRequestStartLine: %s\n",CkRest_lastRequestStartLine(rest));
printf("LastRequestHeader: %s\n",CkRest_lastRequestHeader(rest));
CkRest_Dispose(rest);
CkStream_Dispose(fileStream);
CkBinData_Dispose(bd);
return;
}
// Save the contents of bd to a file.
success = CkBinData_WriteFile(bd,"qa_output/penguins.jpg");
printf("Success: %d\n",success);
CkRest_Dispose(rest);
CkStream_Dispose(fileStream);
CkBinData_Dispose(bd);
}