【发布时间】:2015-12-01 01:38:06
【问题描述】:
我可能做错了什么,但使用 Curl/Rest 客户端,我可以使用 Oauth2 生成的令牌向服务器发送 http 请求,详细信息如下:
url: https://blah.blah
Authorization: OAuth <oauth token>
Accept: application/blah (I'm hiding some of the details...)
使用 Curl/Rest 可以正常工作,并且我按预期返回了 JSON 对象。但是,在 Java 中执行此操作时,我每次都会得到 401。我猜我搞砸了以某种方式附加标题,但我看不出如何。
url = new URL(baseUrl+ reqUrl);
System.out.println("Querying API with the following url:" + url.toString());
httpReq = (HttpURLConnection) url.openConnection();
httpReq.addRequestProperty("Authorization: OAuth ", token.trim());
httpReq.addRequestProperty("Accept", "application/blah...");
httpReq.connect();
我猜我的标题设置有误,但我不知道怎么做。有没有更好的方法来实现 http 请求的标头?
任何意见或建议将不胜感激。 谢谢
【问题讨论】:
标签: java http httpurlconnection