当前位置: 首页>>代码示例>>Java>>正文


Java HttpsURLConnection.setFixedLengthStreamingMode方法代码示例

本文整理汇总了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);
   }
 
开发者ID:ahmed-ebaid,项目名称:invest-stash-rest,代码行数:28,代码来源:HttpRequests.java

示例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();
  }
}
 
开发者ID:XecureIT,项目名称:PeSanKita-lib,代码行数:48,代码来源:PushServiceSocket.java


注:本文中的javax.net.ssl.HttpsURLConnection.setFixedLengthStreamingMode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。