有一个 XML 文件 match.xml,很容易通过 curl 发送 POST 并将其添加到数据库中。
curl -F "xml=@match.xml" web_url
Java 的标准 POST 实现返回 POST 响应代码::400 错误请求
public static void sendPOST(String data) throws IOException {
URL obj = new URL(FinalConstants.BETRADAR_UPLOAD);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(data.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
} else {
System.out.println("POST request not worked");
}
}
并使用这样的设置返回相同的错误
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "text/xml");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
告诉我它是如何工作的curl -F?如何正确发送数据?
更新程序
我用 HttpClient 尝试了几个选项,它们都提供了以下内容
HttpResponseProxy{HTTP/1.1 400 错误请求 [服务器:nginx,日期:2017 年 2 月 9 日星期四 20:07:18 GMT,内容类型:应用程序/八位字节流,内容长度:0,连接:保持活动状态]}
1个选项
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(FinalConstants.UPLOAD);
HttpEntity entity =
new ByteArrayEntity(data.getBytes("UTF-8"),
ContentType.TEXT_XML);
post.setEntity(entity);
HttpResponse response = client.execute(post);
String result = EntityUtils.toString(response.getEntity());
选项 2
InputStream in ;
StringEntity entity = new StringEntity(data, ContentType.create(
"text/xml", Consts.UTF_8));
entity.setChunked(true);
HttpPost httppost = new HttpPost( FinalConstants.UPLOAD);
httppost.setEntity(entity);
HttpClient client = HttpClients.createDefault();
HttpResponse response = client.execute(httppost);
System.out.println(response.toString());
in=response.getEntity().getContent();
String body = IOUtils.toString(in);
System.out.println(body);
也是使用该文件的变体
builder.addBinaryBody(
"file",
new FileInputStream(f),
ContentType.APPLICATION_OCTET_STREAM, // Также как и с другими типами
f.getName()
);
都一样,我想发送一个字符串,而不是一个文件
UPD2
public static void sendPOST(File file) throws IOException {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
final HttpPost uploadFile = new HttpPost(FinalConstants.UPLOAD);
final HttpEntity entity = MultipartEntityBuilder
.create()
.addBinaryBody("xml", file).build();
uploadFile.setEntity(entity);
final HttpResponse response = httpClient.execute(uploadFile);
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
java.lang.NoSuchMethodError: org.apache.http.entity.ContentType.create(Ljava/lang/String;[Lorg/apache/http/NameValuePair;)Lorg/apache/http/entity/ContentType;
at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:219)
at org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:240)
curl -F模拟发送 HTTP 表单,"xml=@match.xml"- 附加具有指定名称的文件。对于此类 HTTP 请求(带有附件),Content-Type 必须是
multipart/form-data,并且此类请求的一部分(部分)必须具有名称xml(对于您的示例)并包含具有相应 Content-Type 的文件.老实说,正如问题评论中已经建议的那样,使用 Apache 的 HttpClient 库更容易。
有很多关于如何
multipart/form-data使用 HttpClient 发送的示例,这是来自 Google 的第一个示例:https ://stackoverflow.com/questions/1378920/how-can-i-make-a-multipart-form-data-post-请求使用java编辑:此代码提供与相同的查询
curl -F "xml=@match.xml" web_url您还需要添加以下依赖项(Maven):