【问题标题】:pass json string to Post method将 json 字符串传递给 Post 方法
【发布时间】:2017-04-19 12:15:41
【问题描述】:

我有一个 json 字符串,我想将它传递给 POST 方法。但是 'execute' 和 'executeMethod ' 抛出错误如下:

“HttpClient 类型中的方法 execute(HttpUriRequest) 不适用于参数 (PostMethod)”。我已经包括了依赖项。

我的代码:

StringRequestEntity requestEntity = new StringRequestEntity(
            json-string,
            "application/json",
            "UTF-8");
PostMethod postMethod = new PostMethod("myUrl");
postMethod.setRequestEntity(requestEntity);
HttpResponse response = httpclient.execute(postMethod);

有没有其他方法可以做到这一点?请帮忙。提前致谢

【问题讨论】:

    标签: java post http-post


    【解决方案1】:

    我使用Apache HttpClient。 调用 post 方法的代码片段如下。

    String JSON_STRING="{"name":"Example"}";
    StringEntity requestEntity = new StringEntity(
    JSON_STRING,ContentType.APPLICATION_JSON);
    HttpPost postMethod = new HttpPost("http://example.com/action");
    postMethod.setEntity(requestEntity);
    HttpResponse rawResponse = httpclient.execute(postMethod);
    

    【讨论】:

    • 与 Apache HttpClient 也一样,我得到同样的错误,“方法 execute(HttpPost) 未定义类型 HttpClient”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    相关资源
    最近更新 更多