gpt4 book ai didi

java - 构造 http POST 请求的问题

转载 作者:可可西里 更新时间:2023-11-01 16:38:51 25 4
gpt4 key购买 nike

我可以在 SOAP UI 中执行 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 ui 中 c/p payload :

<?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>

然后我在附件选项卡中添加附件。 NameContent-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);

所以我将payload设置为post请求的字符串,但是我不能同时添加文件附件。

这是来自 soap 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

不能复制相同的行为

最佳答案

在您的示例中,您没有将 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-body”的值是什么,但是 multipart/form-data 请求的每个部分都需要有一个名称,所以如果你试图发送一个文件并且在单个请求中包含一些 XML,您需要同时提供文件部分和 XML 部分名称。

A "multipart/form-data" message contains a series of parts, each representing a successful control.Each part is expected to contain a name attribute specifying the control name of the corresponding control.

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

关于java - 构造 http POST 请求的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11422141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com