2009-11-07 32 views
3

我正嘗試連接一個桌面應用程序,我正在使用del.icio.us api @Delicious API編寫,只需向他們提供我的用戶名和密碼,並請求一個URL將書籤發佈到我的配置文件。Java HTTP AUTH?

我的問題是,我不明白如何在打開連接時發送我的登錄憑據。

我該如何去做這件事?

+0

你的意思是HTTP基本身份驗證? – 2009-11-07 16:05:20

+0

如果您查看api的鏈接,那麼必須在我的瀏覽器中鍵入以添加書籤的示例鏈接是https://api.del.icio.us/v1/posts/add&url=http://www .google.com&description = awesome – AFK 2009-11-07 16:08:44

+0

看到我更新的答案:) – 2009-11-07 16:12:44

回答

2

來源網站您引用了:

所有/ v1 api都需要https請求和HTTP-Auth。

HTTP-Auth是用於基本認證的標頭。
在Java中,你可以簡單地把你的憑據的網址:

http://user:[email protected]/ 

您可以驗證它被設置正確使用方法URL.getUserInfo()

0

如果你只需要HTTP基本身份驗證,就可以形成你的URL是這樣的:

http://user:[email protected] 

UPDATE:

使用提供的例子,這應該工作:

https://user:[email protected]/v1/posts/add&url=http://www.google.com&description=awesome 
+0

顯然這不起作用了..至少不是我所做的帳戶。我認爲這與雅虎最近接管美味 – AFK 2009-11-07 16:39:25

-2

我想這對於美味的API和它的工作就像一個魅力

import urllib2,base64 
req=urllib2.Request("https://api.del.icio.us/v1/posts/update") 
username="Replace with your Username" 
password="Replace with your password" 
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') 
req.add_header("Authorization", "Basic %s" % base64string) 
response=urllib2.urlopen(req) 
print response.read() 
+0

我很確定提問者想要一個java的答案,因爲這是python代碼,我真的不知道它會如何幫助。 請再次閱讀問題。 – Willy 2012-11-05 15:13:53

0

這答案是確定的:

public String reloadTomcatWebApplication(String user, String pwd, String urlWithParameters, boolean returnResponse){   
    URL url = null; 
    try { 

     url = new URL(urlWithParameters); 
    } catch (MalformedURLException e) { 
     System.out.println("MalformedUrlException: " + e.getMessage()); 
     e.printStackTrace(); 
     return "-1"; 
    } 

    URLConnection uc = null; 
    try { 
     uc = url.openConnection(); 
    } catch (IOException e) { 
     System.out.println("IOException: " + e.getMessage()); 
     e.printStackTrace(); 
     return "-12"; 
    } 


    String userpass = user + ":" + pwd; 
    String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes()); 

    uc.setRequestProperty ("Authorization", basicAuth); 

    InputStream is = null; 
    try { 
     is = uc.getInputStream(); 
    } catch (IOException e) { 
     System.out.println("IOException: " + e.getMessage()); 
     e.printStackTrace(); 
     return "-13"; 
    } 
    if(returnResponse){ 
    BufferedReader buffReader = new BufferedReader(new InputStreamReader(is)); 
    StringBuffer response = new StringBuffer(); 

    String line = null; 
    try { 
     line = buffReader.readLine(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return "-1"; 
    } 

    while (line != null) { 
     response.append(line); 
     response.append('\n'); 
     try { 
      line = buffReader.readLine(); 
     } catch (IOException e) { 
      System.out.println(" IOException: " + e.getMessage()); 
      e.printStackTrace(); 
      return "-14"; 
     } 
    } 

    try { 
     buffReader.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return "-15"; 
    }  
    System.out.println("Response: " + response.toString()); 
    return response.toString(); 

    }    
    return "0"; 
}