java 字节 [] 的 HTTPClient PostMethod

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5799847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:49:18  来源:igfitidea点击:

HTTPClient PostMethod for byte[]

javaapache-commons-httpclient

提问by c12

I need to send a byte[] to rest web service end point and I was wondering how to setup the request using HTTPClient's PostMethod, any ideas?

我需要发送一个字节 [] 来休息 Web 服务端点,我想知道如何使用 HTTPClient 的 PostMethod 设置请求,有什么想法吗?

回答by Anders Lindahl

ByteArrayEntityshould be what your're looking for:

ByteArrayEntity应该是你正在寻找的:

 [...]
 PostMethod post = new PostMethod(url);
 post.setRequestEntity(new ByteArrayEntity(bytes));
 post.setRequestHeader("Content-type", "application/octet-stream");
 [...]

You will have to set content-typeto match what you have in the byte array.

您必须设置content-type以匹配字节数组中的内容。