java.net.http.HttpRequest#BodyPublisher ( )源码实例Demo

下面列出了java.net.http.HttpRequest#BodyPublisher ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Java-Coding-Problems   文件: FormBodyPublisher.java
public static HttpRequest.BodyPublisher ofForm(Map<Object, Object> data) {

        StringBuilder body = new StringBuilder();

        for (Object dataKey : data.keySet()) {

            if (body.length() > 0) {
                body.append("&");
            }

            body.append(encode(dataKey))
                    .append("=")
                    .append(encode(data.get(dataKey)));
        }

        return HttpRequest.BodyPublishers.ofString(body.toString());
    }
 
源代码2 项目: Fibry   文件: HttpChannel.java
private static HttpRequest.BodyPublisher dataAsPublisher(String remoteActorNameEncoded, String objectTypeEncoded, String messageEncoded) {
    var builder = new StringBuilder();

    builder.append("actorName=");
    builder.append(remoteActorNameEncoded);
    builder.append("&type=");
    builder.append(objectTypeEncoded);
    builder.append("&message=");
    builder.append(messageEncoded);

    return HttpRequest.BodyPublishers.ofString(builder.toString());
}
 
源代码3 项目: keycloak-extension-playground   文件: Main.java
public static HttpRequest.BodyPublisher ofFormData(Map<Object, Object> data) {
    var builder = new StringBuilder();
    for (Map.Entry<Object, Object> entry : data.entrySet()) {
        if (builder.length() > 0) {
            builder.append("&");
        }
        builder.append(URLEncoder.encode(entry.getKey().toString(), StandardCharsets.UTF_8));
        builder.append("=");
        builder.append(URLEncoder.encode(entry.getValue().toString(), StandardCharsets.UTF_8));
    }
    return HttpRequest.BodyPublishers.ofString(builder.toString());
}
 
public static HttpRequest.BodyPublisher ofMultipart(
        Map<Object, Object> data, String boundary) throws IOException {

    final byte[] separator = ("--" + boundary + LINE_SEPARATOR
            + "Content-Disposition: form-data; name=").getBytes(StandardCharsets.UTF_8);
    final List<byte[]> body = new ArrayList<>();

    for (Object dataKey : data.keySet()) {
        body.add(separator);

        Object dataValue = data.get(dataKey);

        if (dataValue instanceof Path) {
            Path path = (Path) dataValue;

            String mimeType = fetchMimeType(path);

            body.add(("\"" + dataKey + "\"; filename=\"" + path.getFileName()
                    + "\"" + LINE_SEPARATOR + "Content-Type: "
                    + mimeType + LINE_SEPARATOR + LINE_SEPARATOR)
                    .getBytes(StandardCharsets.UTF_8));

            body.add(Files.readAllBytes(path));

            body.add(LINE_SEPARATOR.getBytes(StandardCharsets.UTF_8));
        } else {
            body.add(("\"" + dataKey + "\""
                    + LINE_SEPARATOR + LINE_SEPARATOR + dataValue + LINE_SEPARATOR)
                    .getBytes(StandardCharsets.UTF_8));
        }
    }

    body.add(("--" + boundary + "--").getBytes(StandardCharsets.UTF_8));

    return HttpRequest.BodyPublishers.ofByteArrays(body);
}
 
源代码5 项目: BHBot   文件: DiscordManager.java
public HttpRequest.BodyPublisher build() {
    if (partsSpecificationList.size() == 0) {
        throw new IllegalStateException("Must have at least one part to build multipart message.");
    }
    addFinalBoundaryPart();
    return HttpRequest.BodyPublishers.ofByteArrays(PartsIterator::new);
}
 
源代码6 项目: alf.io   文件: HttpUtils.java
public static <K, V> HttpRequest.BodyPublisher ofFormUrlEncodedBody(Map<K, V> data) {
    Objects.requireNonNull(data);
    StringBuilder sb = new StringBuilder();
    data.forEach((k,v) -> {
        if (sb.length() > 0) {
            sb.append("&");
        }
        sb.append(URLEncoder.encode(k.toString(), StandardCharsets.UTF_8)).append("=").append(URLEncoder.encode(v.toString(), StandardCharsets.UTF_8));
    });
    return HttpRequest.BodyPublishers.ofString(sb.toString());
}
 
源代码7 项目: alf.io   文件: HttpUtils.java
public HttpRequest.BodyPublisher build() {
    if (partsSpecificationList.size() == 0) {
        throw new IllegalStateException("Must have at least one part to build multipart message.");
    }
    addFinalBoundaryPart();
    return HttpRequest.BodyPublishers.ofByteArrays(PartsIterator::new);
}
 
源代码8 项目: alf.io   文件: SimpleHttpClient.java
private static HttpRequest.BodyPublisher buildRequestBody(Object body) {
    return body == null ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(Json.GSON.toJson(body));
}