Java
Chilkat2 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.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.Stream;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Rest rest = new Rest();
// Connect to the web server
boolean bTls = true;
int port = 443;
boolean bAutoReconnect = true;
try {
rest.connect("www.chilkatsoft.com", port, bTls, bAutoReconnect);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Setup a file stream for the download
Stream fileStream = new Stream();
fileStream.setSinkFile("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 = null;
try {
responseStr = rest.fullRequestNoBody("GET", "/images/starfish.jpg");
} catch (ChilkatException e) {
// Examine the request/response to see what happened.
System.out.println("response status code = " + rest.getResponseStatusCode());
System.out.println("response status text = " + rest.getResponseStatusText());
System.out.println("response header: " + rest.getResponseHeader());
System.out.println("response body (if any): " + responseStr);
System.out.println("---");
System.out.println("LastRequestStartLine: " + rest.getLastRequestStartLine());
System.out.println("LastRequestHeader: " + rest.getLastRequestHeader());
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.
BinData bd = new BinData();
try {
rest.fullRequestNoBodyBd("GET", "/images/penguins.jpg", bd);
} catch (ChilkatException e) {
// Examine the request/response to see what happened.
System.out.println("response status code = " + rest.getResponseStatusCode());
System.out.println("response status text = " + rest.getResponseStatusText());
System.out.println("response header: " + rest.getResponseHeader());
System.out.println("response body (if any): " + bd.getString("utf-8"));
System.out.println("---");
System.out.println("LastRequestStartLine: " + rest.getLastRequestStartLine());
System.out.println("LastRequestHeader: " + rest.getLastRequestHeader());
return;
}
// Save the contents of bd to a file.
try {
bd.writeFile("qa_output/penguins.jpg");
success = true;
} catch (ChilkatException e) {
success = false;
}
System.out.println("Success: " + success);
}
}