Sending Name Value Pair in POST using Jersey Client

本小妞迷上赌 提交于 2019-12-12 15:45:06

问题


How can i pass name value pairs as body to a POST ReST Service in Jersey. Something similar to the code below using Apache Commons PostMethod

    final PostMethod post = new PostMethod(url);
    post.setRequestBody(new NameValuePair[] {
            new NameValuePair("loginId", userId),
            new NameValuePair("logonPassword", password),
            new NameValuePair("signature", signature),
            new NameValuePair("timestamp", timestamp),
            new NameValuePair("sourceSiteId", sourceSiteId) });

I'm porting this call to my application. The current call uses apache commons PostMethod. In my application i used Jersey. So i want to use the jersey classes/features instead of apache.


回答1:


There is a MultivaluedMap interface in JAX-RS with a 'MultivaluedMapImpl' in Jersey.

Client client = Client.create();
WebResource webResource = client.resource("http://site.com/resource");
MultivaluedMap<String, String> map = new MultivaluedMapImpl();
map.put("loginId", loginId);
...
ClientResponse response = webResource.type("application/x-www-form-urlencoded")
             .post(ClientResponse.class, map);

Here is a more comprehensive example of how to use Jersey client API.



来源:https://stackoverflow.com/questions/12529714/sending-name-value-pair-in-post-using-jersey-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!