RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 625652
Accepted
Senior Pomidor
Senior Pomidor
Asked:2020-02-09 20:46:23 +0000 UTC2020-02-09 20:46:23 +0000 UTC 2020-02-09 20:46:23 +0000 UTC

POST xml 错误请求 400

  • 772

有一个 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)
java
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Sergi
    2020-02-10T01:24:15Z2020-02-10T01:24:15Z

    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

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            final HttpPost uploadFile = new HttpPost("web_url");
            final HttpEntity entity = MultipartEntityBuilder.create().addBinaryBody("xml", new File("match.xml")).build();
            uploadFile.setEntity(entity);
            final HttpResponse response = httpClient.execute(uploadFile);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    

    您还需要添加以下依赖项(Maven):

    <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.3</version>
        </dependency>
    
    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5