2011-02-24 27 views
4

因此,我正在使用面向Java的Signpost OAuth库。我遇到了一些使用Apache Commons HTTP库的复杂问题。看看下面的代码:具有路标和Apache Commons HTTP的OAuth

URL url = new URL("http://api.neoseeker.com/forum/get_pm_counts.json"); 
HttpRequest request = (HttpRequest) url.openConnection(); 

consumer.sign(request); 

request.connect(); 

System.out.println("Response: " + request.getResponseCode() + " " 
     + request.getResponseMessage()); 

这是取自this example。您可以看到request曾经是HttpURLConnection,但因为我将使用Apache Commons HTTP库,所以我将其更改为HttpRequest对象。现在,当我拨打connect(),getResponseCode()getResponseMessage()时,出现错误,因为这些功能用于HttpURLConnection。我会使用HttpRequest的哪些函数,以便我能够正确编译和运行代码?谢谢!

回答

6

Signpost有一个独立的模块用于使用Apache的HTTP客户端。您需要为此使用CommonsHttpOAuthConsumer。

该模块住在这里 - http://code.google.com/p/oauth-signpost/downloads/detail?name=signpost-commonshttp4-1.2.1.1.jar&can=2&q=

一些示例代码,使用它是在这里 - http://code.google.com/p/oauth-signpost/wiki/ApacheCommonsHttp4,但它只是归结为实例,而不是正常的一个CommonsHttpOAuthConsumer ...

如果你用maven ,坐标在这里,我无法找到他们记录在任何地方,当我试图找出这个...

<dependency> 
    <groupId>oauth.signpost</groupId> 
    <artifactId>signpost-commonshttp4</artifactId> 
    <version>1.2.1.1</version> 
</dependency> 
+0

感谢吨的maven坐标。你从哪里找到这些? – Jazzepi 2012-08-02 18:09:31

+0

Maven中央回购搜索引擎http://search.maven.org/。 – mblinn 2012-08-03 02:36:27