java httpcomponents_java – 使用OAuth-Signpost和Apache HttpComponents签署POST请求的正确方法是什么?...

我正在使用OAuth-Signpost

Java库来签署从客户端发送到实现OAuth身份验证的服务器的请求.当GET请求(使用HttpURLConnection)时,一切都正常:请求被签名,参数被包括,签名在目的地匹配.但是,它似乎不适用于POST请求.我知道在使用HttpURLConnection签名POST时可能出现的问题,所以我转移到Apache HttpComponents库以获得这些请求.在以下示例中发送的参数是纯字符串和类似XML的字符串(“rxml”).我的代码如下:

public Response exampleMethod(String user,String sp,String ep,String rn,String rxml){

//All these variables are proved to be correct (they work right in GET requests)

String uri = "...";

String consumerKey = "...";

String consumerSecret = "...";

String token = "...";

String secret = "...";

//create the parameters list

List params = new ArrayList();

params.add(new BasicNameValuePair("user",user));

params.add(new BasicNameValuePair("sp",sp));

params.add(new BasicNameValuePair("ep",ep));

params.add(new BasicNameValuePair("rn",rn));

params.add(new BasicNameValuePair("rxml",rxml));

// create a consumer object and configure it with the access

// token and token secret obtained from the service provider

OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret);

consumer.setTokenWithSecret(token,secret);

// create an HTTP request to a protected resource

HttpPost request = new HttpPost(uri);

// sign the request

consumer.sign(request);

// set the parameters into the request

request.setEntity(new UrlEncodedFormEntity(params));

// send the request

HttpClient httpClient = new DefaultHttpClient();

HttpResponse response = httpClient.execute(request);

//if request was unsuccessful

if(response.getStatusLine().getStatusCode()!=200){

return Response.status(response.getStatusLine().getStatusCode()).build();

}

//if successful,return the response body

HttpEntity resEntity = response.getEntity();

String responseBody = "";

if (resEntity != null) {

responseBody = EntityUtils.toString(resEntity);

}

EntityUtils.consume(resEntity);

httpClient.getConnectionManager().shutdown();

return Response.status(200).entity(responseBody).build();

}

当我向服务器发送POST请求时,我得到一个错误,告诉我签名(我发送的一个和服务器自己计算的一个)不匹配,所以我想这与签名的基本字符串有关以及POST签名的工作方式,因为它们正在处理双方的密钥和秘密(已选中).

我已经看到,通过这种方式将参数设置为URL的一部分(如在GET请求中).这对我来说不行,因为XML参数可能会超过URL长度,因此需要作为POST参数发送.

我想我在签名POST请求或处理参数时出错了,但我不知道是什么.请你帮帮我吗

P.S:如果我缺乏关于这个问题的上下文,错误跟踪或其他信息,我很抱歉,但我在这里是新手.所以请不要犹豫,要求我更多的信息,如果你需要它.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值