我想从curl实现HTTP post。curl $CURL_OPTS -X POST --data-binary {} -s -K <(cat <<< "-u micromdm:$API_TOKEN") "$SERVER_URL/$endpoint"
上面的curl命令发送空的二进制数据。
我知道如何发送字符串数据。
- 正在发送字符串数据**
try {
CloseableHttpClient httpClient = null;
try {
httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
httpPost.addHeader("Accept", "application/json;charset=utf-8");
httpPost.addHeader("Authorization", "XXXXXXXX");
StringEntity se = new StringEntity(jsonstring, "UTF-8");
httpPost.setEntity(se);
HttpResponse httpResponse = httpClient.execute(httpPost);
catch () {
}
catch () {
}
但是如何发送空的二进制数据呢?
尝试
ByteArrayEntity byteArrayEntity = [];
httpPost.setEntity(byteArrayEntity, ContentType.DEFAULT_BINARY);
这不是工作。
我看了一些文章,这是不正确的解决方案。
http-post-in-java-with-file-upload
how-to-get-raw-binary-data-from-a-post-request-processed-by-spring
uploading-binary-data-with-httppost
httpclient-post-http-request
有很多类似的相关文章,但我没有找到解决方案。谁能给我这个代码样本?谢谢。
1条答案
按热度按时间2izufjch1#
字节[]空数据=新字节[0];
httpPost =新的httpPost(“http://example.com/upload“);
设置实体(新字节数组实体(空数据));
HttpClient =新的默认HttpClient();http响应= http客户端.执行(http发布);