我有一个休息网络服务,它采用带有多部分消息的 POST 方法:
@Path("transferFile")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_XML)
public String multipartTest(com.sun.jersey.multipart.MultiPart data) {
try {
// get first body part (index 0)
BodyPart bp = multiPart.getBodyParts().get(0);
etc..
现在我正在尝试为此编写一个java客户端。我从一个简单的球衣客户端开始:查看纯副本到剪贴板打印?
MultiPart multiPart = new MultiPart();
multiPart.bodyPart( new BodyPart(wavestream,MediaType.APPLICATION_OCTET_STREAM_TYPE));
Client c = Client.create();
WebResource r = c.resource("http://127.0.0.1:8080/webapp:);
response=r.path("transferFile").type(MediaType.MULTIPART_FORM_DATA).accept(MediaType.APPLICATION_XML).post(String.class, multiPart);
这很好用 - 一切都很好。但是我需要这个客户端在 Android 上工作,我在那个平台上使用球衣时遇到了麻烦。所以我用正常的方式在android上发送多部分消息:
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.socket.timeout", new Integer(90000)); // 90 second
HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/webapp/transferFile");
httpPost.setHeader("Content-Type", MediaType.MULTIPART_FORM_DATA );
//tried with and without base64
byte [] encodedWavestream = Base64.encodeBytesToBytes(wavestream);
InputStream ins = new ByteArrayInputStream(encodedWavestream);
InputStreamBody body = new InputStreamBody(ins, "test" );
int send = ins.available();
MultipartEntity requestContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE );
requestContent.addPart("stream", body);
httpPost.setEntity(requestContent);
HttpResponse Response = client.execute(httpPost);
这给出了来自服务器的恼人响应:
HTTP Status 400 - Bad Request
The request sent by the client was syntactically incorrect (Bad Request).
我检查了服务器日志文件,但那里什么也没有。所以我不知道这个错误的根源是什么。我写了一个简单的 html 页面,其中包含一个帖子公式和“multipart/form-data”内容类型,它也可以工作!来自soapUI 的自动生成请求也有效!为什么我的客户端不工作?有人可以帮忙吗?