下面列出了java.net.http.HttpRequest#BodyPublisher ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
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());
}
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);
}
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);
}
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());
}
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);
}
private static HttpRequest.BodyPublisher buildRequestBody(Object body) {
return body == null ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(Json.GSON.toJson(body));
}