2010-06-22 154 views
0

我需要從commons-httpclient-3.0.jar傳遞給commons-httpclient-3.1.jar 但更改jar我的代碼不再工作。 問題是新庫自動對傳入的uri進行編碼。 有沒有辦法避免這種情況? 我必須與雅虎API進行交互,我不得編碼URI,否則我無法訪問服務。 這裏有一段我​​的代碼,比較兩條印刷線,我觀察到了傳遞的URI和使用的URI之間的區別。使用commons-httpclient-3.X.jar的Java問題

GetMethod getMethod = new GetMethod(); 
    try { 
     URI uri = new URI(DeliciousApi.generateRequestToken(), false); 
     getMethod.setURI(uri); 
     System.out.println("Passed URI: " + uri.getURI()); 
     int statusCode = client.executeMethod(getMethod); 
     if (statusCode != HttpStatus.SC_OK) { 
      System.out.println("Used URI: " + getMethod.getURI()); 
      System.err.println("getMethod failed: " + getMethod.getStatusLine()); 
     } 

而這是輸出:

Passed URI: https://api.login.yahoo.com/oauth/v2/get_request_token?oauth_nonce=ce4630523j788f883f76314ed3965qw9&oauth_timestamp=1277236486&oauth_consumer_key=hd7sHfs5YVFuh3DRTUFgFgF7GcF4RDtsTXStGdRyJJf7WSuShQAShd2JdiwjIibHsU8YFDgshk7hd32xjA6isnNsT7SkbLS8YDHy&oauth_signature_method=plaintext&oauth_signature=53h8x475a66v238j7f43456lhhgg8s7457fwkkdd%26&oauth_version=1.0&xoauth_lang_pref="en-us"&oauth_callback=oob 
Used URI: https://api.login.yahoo.com/oauth/v2/get_request_token?oauth_nonce=ce4630523j788f883f76314ed3965qw9&oauth_timestamp=1277236486&oauth_consumer_key=hd7sHfs5YVFuh3DRTUFgFgF7GcF4RDtsTXStGdRyJJf7WSuShQAShd2JdiwjIibHsU8YFDgshk7hd32xjA6isnNsT7SkbLS8YDHy&oauth_signature_method=plaintext&oauth_signature=53h8x475a66v238j7f43456lhhgg8s7457fwkkdd%2526&oauth_version=1.0&xoauth_lang_pref=%22en-us%22&oauth_callback=oob 

getMethod失敗:HTTP/1.1 401禁止

coppia:oauth_problem signature_invalid

particolarly:

%26 & oauth_version - >%2526 & oauth_version

xoauth_lang_pref = 「EN-US」 - > xoauth_lang_pref =%22en美%22

回答

0

你可以這樣做避免編碼,

 URI uri = new URI(DeliciousApi.generateRequestToken(), true); 

然而,你可能會得到你原來的URL,這是例外沒有正確編碼。您需要編碼雙引號。更好的是,擺脫它。

+0

非常感謝您的提示!這解決了我的麻煩!我很愚蠢,因爲我從來沒有購買過! :-) – AndyPower 2010-06-23 21:42:46

0
插入串這裏使用setUri會( 「https://api.login.yahoo.com/oauth/v2/get_request_token」),其次是setQueryString

)工作?我似乎記得有過的查詢字符串做這種方式更多的控制權......

+0

謝謝你的答案,但我已經嘗試過這種解決方案之前,結果是一樣的。 在我的項目中,我必須使用api commons-httpclient-3.1.jar,但我可以更改調用yahoo服務的代碼。有沒有可以用來與雅虎互動的另一個API? – AndyPower 2010-06-22 22:11:56