我正在尝试实现(在Java中)POST请求的POSTMAN预览。它使用边界和内容处置,我不太熟悉Java中的边界和内容处置实现。我想专家们可以帮助我解决这个问题,谢谢。
POST /etap-cgi/cgiunl.exe HTTP/1.1
Host: unl.ru
X-Auth-Token: 5dc347210229bd8b9565d213f0d4f80f738970083d03a8e73a0d5f649019272e
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="conversion"
true
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="language"
en
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="data"
aar partasi nah
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="outputmode"
text
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="coding"
utf-8
----WebKitFormBoundaryE19zNvXGzXaLvS5C
我已经尝试过,但它说'
翻译过程中出现致命错误
这是我在Java中的实现。
HttpClient httpclient = new DefaultHttpClient();
try {
// url parameters
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
.create();
multipartEntity.addTextBody("conversion", "ture",
ContentType.TEXT_PLAIN);
multipartEntity.addTextBody("language", "en",
ContentType.TEXT_PLAIN);
multipartEntity.addTextBody("data", "example",
ContentType.TEXT_PLAIN);
multipartEntity.addTextBody("outputmode", "text",
ContentType.TEXT_PLAIN);
multipartEntity.addTextBody("coding", "utf-8",
ContentType.TEXT_PLAIN);
multipartEntity
.setBoundary("----WebKitFormBoundaryE19zNvXGzXaLvS5C");
HttpEntity multiPart = multipartEntity.build();
HttpPost httpPost = new HttpPost(
url);
httpPost.setEntity(multiPart);
httpPost.setHeader("Content-Disposition", "form-data");
// get response after execution
HttpResponse response = httpclient.execute(httpPost);
// get response entities
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: "
+ resEntity.getContentLength());
String responseBody = EntityUtils.toString(resEntity);
System.out.println("Data: " + responseBody);
}
EntityUtils.consume(resEntity);
} catch (Exception err) {
System.err.println(err.toString());
} finally {
// close connection
httpclient.getConnectionManager().shutdown();
}
最佳答案
我已经通过上载文件而不是由多部分字符串解决了我的问题,这是带有边界参数的解决方案,其中没有“ WebKitFormBoundary”字符。希望这对某人有帮助。
public String httpOperation(String conversion, String fileName) {
HttpClient httpclient = new DefaultHttpClient();
try {
// url parameters
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
.create();
// add html tags param
multipartEntity.addTextBody("conversion", conversion,
ContentType.TEXT_PLAIN);
multipartEntity.addTextBody("language", EN, ContentType.TEXT_PLAIN);
multipartEntity.addTextBody("outputmode", TEXT,
ContentType.TEXT_PLAIN);
multipartEntity.addTextBody("coding", UTF, ContentType.TEXT_PLAIN);
// add files as attachments
multipartEntity.addPart("sourcefile", new FileBody(new File(
fileName), ContentType.TEXT_PLAIN, "filename"));
multipartEntity.setBoundary(PARAM_BOUNDARY);
HttpEntity postEntity = multipartEntity.build();
HttpPost httpPost = new HttpPost(URL);
httpPost.setHeader("Content-Disposition", "form-data;");
httpPost.setEntity(postEntity);
// get response after execution
HttpResponse response = httpclient.execute(httpPost);
// get response entities
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
@SuppressWarnings("deprecation")
String responseBody = EntityUtils.toString(resEntity,
HTTP.UTF_8);
// print output
if (conversion.equals(TRUE)) {
extractResponse(responseBody.toString());
// System.out.println(responseBody.toString());
result = responseBody.toString();
} else {
// System.out.println(responseBody.toString());
result = responseBody.toString();
}
}
EntityUtils.consume(resEntity);
} catch (Exception err) {
System.err.println(err.toString());
} finally {
// close connection
httpclient.getConnectionManager().shutdown();
}
return result;
}
这是POSTMAN构建预览
POST /url/ HTTP/1.1
Host: unl.ru
X-Auth-Token: 5dc347210229bd8b9565d213f0d4f80f738970083d03a8e73a0d5f649019272e
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="conversion"
false
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="language"
en
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="sourcefile"; filename="conversion.txt"
Content-Type: text/plain
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="outputmode"
text
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="coding"
utf-8
----WebKitFormBoundaryE19zNvXGzXaLvS5C
关于java - 将POSTMAN代码转换成Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32308570/