我能够在SOAP中做一个POST请求。但是我不能用java做同样的事情,我尝试了5-6个小时,却找不到胜利的组合。下面是我要做的文章的xsd模式部分:
<method name="POST">
<request>
<param name="username" style="query" type="xs:string"/>
<param name="id" style="query" type="xs:long"/>
<representation mediaType="multipart/form-data"/>
</request>我只是soap中的c/p有效负载:
<?xml version="1.0" encoding="UTF-8"?>
<ImageList xmlns="http://someurl/1.0/image" >
<Image>
<Name>sampler.jpg</Name>
<Filename>C:\\sampler.jpg</Filename>
<Label>
<Value>Test image</Value>
</Label>
<ImageMetadata>
<Format>jpg</Format>
<Height>300</Height>
<Width>400</Width>
</ImageMetadata>
</Image>
</ImageList>然后在附件选项卡中添加附件。Name、Content-Type等--我得到了有效的响应代码--但是,我没有设法对java做同样的操作,下面是我得到的:
HttpRequestBase post = new HttpPost();
HttpClient client = new DefaultHttpClient();
try {
post.setURI(new URI(URL));
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// set number of retries
post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false));
HttpResponse response = null;
HttpPost post = (HttpPost) method;
try {
post.setURI(new URI(URL));
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileBody uploadFilePart = new FileBody(new File("C:\\sampler.jpg"));
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("upload-file", uploadFilePart);
//payload
String requestBody = fileToString("src/main/resources/imageBodyPayload.xml");
HttpParams parameters = new BasicHttpParams();
parameters.setLongParameter("id", 951);
parameters.setParameter("username", "test");
post.setParams(parameters);
post.setEntity(new StringEntity(requestBody, "multipart/form-data", HTTP.UTF_8));
response = client.execute(post);因此,我将有效负载设置为post请求的字符串,但不能同时添加文件附件。
来自soap的原始请求是这样的:
POST http://localhost:9080/imageUpload/?id=951&userame=test HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: multipart/form-data; boundary="----=_Part_9_23652504.1341953390382"
MIME-Version: 1.0不能复制同样的行为
发布于 2012-08-23 21:55:10
在您的示例中,您没有将MultipartEntity附加到HttpPost请求,这就是文件不上传的原因。也许可以试试这样的方法:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
MultipartEntity reqEntity = new MultipartEntity();
String requestBody = fileToString("src/main/resources/imageBodyPayload.xml");
reqEntity.addPart("request-body", new StringBody(requestBody));
FileBody fileBody = new FileBody(new File("C:\\sampler.jpg"));
reqEntity.addPart("upload-file", fileBody);
HttpParams parameters = new BasicHttpParams();
parameters.setLongParameter("id", 951);
parameters.setParameter("username", "test");
httppost.setParams(parameters);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);我不知道在您的情况下“request”的值是什么,但是多部分/表单-数据请求的每个部分都需要有一个名称,所以如果您试图在单个请求中发送一个文件和一些XML,您需要同时给出文件部分和XML部件名。
一个“多部分/表单数据”消息包含一系列的部分,每个部分代表一个成功的控件。每个部分都应该包含一个name属性,指定相应控件的控件名称。 http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
https://stackoverflow.com/questions/11422141
复制相似问题