POST JSON,不使用java指定密钥

时间:2014-08-02 12:34:32

标签: java json http-post

我有一个客户端提供的URL,客户端希望我使用一些JSON对该URL进行HTTP POST。但问题是他没有给我钥匙,所以我可以发布像key = json string这样的数据。

我问他,但他坚持认为这是可能的。我想知道如果可能的话,我该如何在java中做到这一点。

编辑:

以下是使用Apache Http Client发送http post的简单方法:

import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;


public class PostExample {
    public static void main(String[] args){
        String url = "http://www.google.com";
        InputStream in = null;

        try {
            HttpClient client = new HttpClient();
            PostMethod method = new PostMethod(url);

            //Add any parameter if u want to send it with Post req.
            method.addParameter("p", "apple");

            int statusCode = client.executeMethod(method);

            if (statusCode != -1) {
                in = method.getResponseBodyAsStream();
            }

            System.out.println(in);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

这里我们使用:method.addParameter(“p”,“apple”)指定params; 所以有关键的“p”和价值“苹果”。现在,如果没有密钥,我们只想“推”(json)数据,我们该怎么做? 请我 Java特定回答。

1 个答案:

答案 0 :(得分:2)

您似乎想要使用application / x-www-form-urlencoded,它具有key = value对。不。

将内容类型设置为application / json,然后将帖子的有效负载设为json 。没有钥匙。

在java中使用commons-httpclient和CXF。

相关问题