我可以在SOAP UI中执行POST请求。但我不能从java做同样的事情,尝试了大约5-6个小时,并且找不到优胜组合。这里是我的文章中,我试图让工作XSD架构部分:构建http POST请求的问题
<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>
肥皂UI我只是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请求,但我不能同时添加文件附件。
这是肥皂UI原始请求的样子:
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
无法复制同样的行为
也许值得与任何HTTP框架,这是标记? – davidfrancis 2012-07-10 22:06:47