Java
Java
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 Java Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkRest rest = new CkRest();
// Connect to the web server
boolean bTls = true;
int port = 443;
boolean bAutoReconnect = true;
success = rest.Connect("www.chilkatsoft.com",port,bTls,bAutoReconnect);
if (success != true) {
System.out.println(rest.lastErrorText());
return;
}
// Setup a file stream for the download
CkStream fileStream = new CkStream();
fileStream.put_SinkFile("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.
int expectedStatus = 200;
rest.SetResponseBodyStream(expectedStatus,true,fileStream);
String responseStr = rest.fullRequestNoBody("GET","/images/starfish.jpg");
if (rest.get_LastMethodSuccess() == false) {
// Examine the request/response to see what happened.
System.out.println("response status code = " + rest.get_ResponseStatusCode());
System.out.println("response status text = " + rest.responseStatusText());
System.out.println("response header: " + rest.responseHeader());
System.out.println("response body (if any): " + responseStr);
System.out.println("---");
System.out.println("LastRequestStartLine: " + rest.lastRequestStartLine());
System.out.println("LastRequestHeader: " + rest.lastRequestHeader());
return;
}
System.out.println("downloaded starfish.jpg");
// Clear the response body stream previously set in the call to SetResponseBodyStream.
rest.ClearResponseBodyStream();
// Download something else, but this time send the response body to bd.
CkBinData bd = new CkBinData();
success = rest.FullRequestNoBodyBd("GET","/images/penguins.jpg",bd);
if (success == false) {
// Examine the request/response to see what happened.
System.out.println("response status code = " + rest.get_ResponseStatusCode());
System.out.println("response status text = " + rest.responseStatusText());
System.out.println("response header: " + rest.responseHeader());
System.out.println("response body (if any): " + bd.getString("utf-8"));
System.out.println("---");
System.out.println("LastRequestStartLine: " + rest.lastRequestStartLine());
System.out.println("LastRequestHeader: " + rest.lastRequestHeader());
return;
}
// Save the contents of bd to a file.
success = bd.WriteFile("qa_output/penguins.jpg");
System.out.println("Success: " + success);
}
}