如何使用OAuth-Signpost和Apache HttpComponents验证对RESTful应用程序的POST请求?

时间:2012-09-28 05:31:29

标签: magento rest oauth apache-httpcomponents signpost

我正在使用OAuth-Signpost 1.2来验证对Magento RESTful应用程序的请求。

使用简单的HttpURLConnection很容易实现GET请求,但据我所知,对于POST请求,我还必须使用像Apache HttpComponents HttpClient这样的库。但是,我无法找到正确的方法。

我目前的代码如下:

public static void try2(OAuthConsumer consumer , String API_URL, String postInput) throws Exception{
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(API_URL);

    try {
        StringEntity input = new StringEntity(postInput);
        input.setContentType("application/json");
        postRequest.setEntity(input);
        consumer.sign(postRequest);

        HttpResponse response = httpClient.execute(postRequest);

        if (response.getStatusLine().getStatusCode() != 201) {
            System.out.println("Failure. Code: " + response.getStatusLine().getStatusCode());
        }

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

但我得到的只是错误500.

以更一般的方式,我的问题是:如何使用路标向RESTful应用程序签署POST请求?

0 个答案:

没有答案