【问题标题】:How represente the curl command using the HttpPost: curl --form "tag=mytag" --form "archive=@test.pdf;type=application/pdf"如何使用 HttpPost 表示 curl 命令: curl --form "tag=mytag" --form "archive=@test.pdf;type=application/pdf"
【发布时间】:2014-06-23 14:28:58
【问题描述】:

如何使用 HttpPost 表示 curl 命令:

curl --form "tag=mytag"  --form "archive=@c:/etc/test.pdf;type=application/pdf" ...

下面是我的代码:

HttpPost httpPost = new HttpPost(www.resourceURL.com);
httpPost.setHeader(Authorization: bearer cb084803-ba48-xxxx-xxxx-2a275e199c38)
....

下面是我完成的代码: 但我收到 HTTP 500 错误

    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    HttpPost httpPost = new HttpPost(resourceURL);
    httpPost.setHeader(OAuthConstants.AUTHORIZATION, bearer cb084803-ba48-xxx-xxx-2a275xxx99c38 ));
    httpPost.setHeader(OAuthConstants.CONTENTE_TYPE, OAuthConstants.ENCODED_CONTENT_DATA);
    httpPost.setHeader(OAuthConstants.PARTNERUSERID, login);
    //------------------------------------------------------------------------
    parameters.add(new BasicNameValuePair("title", doc.getTitre()));
    parameters.add(new BasicNameValuePair("tag", doc.getTag()));
    parameters.add(new BasicNameValuePair("archive", "@" + doc.getPath() + ";type=application/pdf"));
    httpPost.setEntity(new UrlEncodedFormEntity(parameters));
    //------------------------------------------------------------------------
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = null;
        response = httpClient.execute(httpPost);
        code = response.getStatusLine().getStatusCode();

我不知道如何解决这个问题

我检查了响应的内容,我得到了这个结果:

[{"error":"bad_request","error_description":"malformed Json : Expected BEGIN_OBJECT but was STRING at line 1 column 1"}]

【问题讨论】:

    标签: java json http curl http-post


    【解决方案1】:

    创建一个UrlEncodedFormEntity 并在那里添加您的表单元素

    List<NameValuePair> formEntries = new ArrayList<>(); // or whatever List sub-type
    formEntries.add(new BasicNameValuePair("tag", "my-tag"));
    // more
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formEntries);
    httpPost.setEntity(entity);
    

    【讨论】:

    • 你可以看到我的回答,告诉我该怎么做
    • @Yanni 不要发帖寻求澄清作为答案。编辑您的问题并向其添加所有相关详细信息。
    • Httpresponsedoes 没有 getErrorStream() 方法我怎么能得到这些信息
    • @Yanni 哎呀,我把图书馆搞混了。只需获取响应实体并检查其内容即可。
    • ContentType=null 和 ContenetEncoding=null
    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2019-02-04
    • 2013-09-25
    相关资源
    最近更新 更多