本文整理匯總了Java中javax.net.ssl.HttpsURLConnection.setFixedLengthStreamingMode方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpsURLConnection.setFixedLengthStreamingMode方法的具體用法?Java HttpsURLConnection.setFixedLengthStreamingMode怎麽用?Java HttpsURLConnection.setFixedLengthStreamingMode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.net.ssl.HttpsURLConnection
的用法示例。
在下文中一共展示了HttpsURLConnection.setFixedLengthStreamingMode方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: httpsPost
import javax.net.ssl.HttpsURLConnection; //導入方法依賴的package包/類
public T httpsPost(String jsonBody, Class<T> clazz) throws IOException {
URL url = new URL(HTTPS_PROTOCOL, ACCOUNT_KEY_SERVICE_HOST, HTTPS_PORT,
ACCOUNT_KEY_ENDPOINT);
httpsConnection = (HttpsURLConnection) url.openConnection();
httpsConnection.setRequestMethod(HttpRequestMethod.POST.toString());
setConnectionParameters(httpsConnection, HttpRequestMethod.POST);
httpsConnection.setFixedLengthStreamingMode(jsonBody.getBytes().length);
try (OutputStreamWriter out = new OutputStreamWriter(
httpsConnection.getOutputStream())) {
out.write(jsonBody);
}
StringBuilder sb = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(
httpsConnection.getInputStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
}
// setFieldNamingPolicy is used to here to convert from
// lower_case_with_underscore names retrieved from end point to camel
// case to match POJO class
Gson gson = new GsonBuilder().setFieldNamingPolicy(
FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
return gson.fromJson(sb.toString(), clazz);
}
示例2: uploadAttachment
import javax.net.ssl.HttpsURLConnection; //導入方法依賴的package包/類
private byte[] uploadAttachment(String method, String url, InputStream data,
long dataSize, byte[] key, ProgressListener listener)
throws IOException
{
URL uploadUrl = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection) uploadUrl.openConnection();
connection.setDoOutput(true);
if (dataSize > 0) {
connection.setFixedLengthStreamingMode((int) AttachmentCipherOutputStream.getCiphertextLength(dataSize));
} else {
connection.setChunkedStreamingMode(0);
}
connection.setRequestMethod(method);
connection.setRequestProperty("Content-Type", "application/octet-stream");
connection.setRequestProperty("Connection", "close");
connection.connect();
try {
OutputStream stream = connection.getOutputStream();
AttachmentCipherOutputStream out = new AttachmentCipherOutputStream(key, stream);
byte[] buffer = new byte[4096];
int read, written = 0;
while ((read = data.read(buffer)) != -1) {
out.write(buffer, 0, read);
written += read;
if (listener != null) {
listener.onAttachmentProgress(dataSize, written);
}
}
data.close();
out.flush();
out.close();
if (connection.getResponseCode() != 200) {
throw new IOException("Bad response: " + connection.getResponseCode() + " " + connection.getResponseMessage());
}
return out.getAttachmentDigest();
} finally {
connection.disconnect();
}
}