A.HttpClient工具包
1.Ruby
rest-open-uri 和 net/http
#!/usr/bin/ruby -w # delicious-open-uri.rb require 'rubygems' require 'rest-open-uri' require 'rexml/document' # Fetches a del.icio.us user's recent bookmarks, and prints each one. def print_my_recent_bookmarks(username, password) # Make the HTTPS request. response = open('https://api.del.icio.us/v1/posts/recent', :http_basic_authentication => [username, password]) # Read the response entity-body as an XML document. xml = response.read # Turn the document into a data structure. document = REXML::Document.new(xml) # For each bookmark... REXML::XPath.each(document, "/posts/post") do |e| # Print the bookmark's description and URI puts "#{e.attributes['description']}: #{e.attributes['href']}" end end # Main program username, password = ARGV unless username and password puts "Usage: #{$0} [username] [password]" exit end print_my_recent_bookmarks(username, password)
2.Python
httplib2
#!/usr/bin/python2.5 # delicious-httplib2.py import sys from xml.etree import ElementTree import httplib2 # Fetches a del.icio.us user's recent bookmarks, and prints each one. def print_my_recent_bookmarks(username, password): client = httplib2.Http(".cache") client.add_credentials(username, password) # Make the HTTP request, and fetch the response and the entity-body. response, xml = client.request('https://api.del.icio.us/v1/posts/recent') # Turn the XML entity-body into a data structure. doc = ElementTree.fromstring(xml) # Print information about every bookmark. for post in doc.findall('post'): print "%s: %s" % (post.attrib['description'], post.attrib['href']) # Main program if len(sys.argv) != 3: print "Usage: %s [username] [password]" % sys.argv[0] sys.exit() username, password = sys.argv[1:] print_my_recent_bookmarks(username, password)
3.Java
HttpClient
// DeliciousApp.java import java.io.*; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; import org.w3c.dom.*; import org.xml.sax.SAXException; import javax.xml.parsers.*; import javax.xml.xpath.*; /** * A command-line application that fetches bookmarks from del.icio.us * and prints them to strandard output. */ public class DeliciousApp { public static void main(String[] args) throws HttpException, IOException, ParserConfigurationException, SAXException, XPathExpressionException { if (args.length != 2) { System.out.println("Usage: java -classpath [CLASSPATH] " + "DeliciousApp [USERNAME] [PASSWORD]"); System.out.println("[CLASSPATH] - Must contain commons-codec, " + "commons-logging, and commons-httpclient"); System.out.println("[USERNAME] - Your del.icio.us username"); System.out.println("[PASSWORD] - Your del.icio.us password"); System.out.println(); System.exit(-1); } // Set the authentication credentials. Credentials creds = new UsernamePasswordCredentials(args[0], args[1]); HttpClient client = new HttpClient(); client.getState().setCredentials(AuthScope.ANY, creds); // Make the HTTP request. String url = "https://api.del.icio.us/v1/posts/recent"; GetMethod method = new GetMethod(url); client.executeMethod(method); InputStream responseBody = method.getResponseBodyAsStream(); // Turn the response entity-body into an XML document. DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(responseBody); method.releaseConnection(); // Hit the XML document with an XPath expression to get the list // of bookmarks. XPath xpath = XPathFactory.newInstance().newXPath(); NodeList bookmarks = (NodeList)xpath.evaluate("/posts/post", doc, XPathConstants.NODESET); // Iterate over the bookmarks and print out each one. for (int i = 0; i < bookmarks.getLength(); i++) { NamedNodeMap bookmark = bookmarks.item(i).getAttributes(); String description = bookmark.getNamedItem("description") .getNodeValue(); String uri = bookmark.getNamedItem("href").getNodeValue(); System.out.println(description + ": " + uri); } System.exit(0); } }
4.C#
System.Web.HTTPWebRequest
using System; using System.IO; using System.Net; using System.Xml.XPath; public class DeliciousApp { static string user = "username"; static string password = "password"; static Uri uri = new Uri("https://api.del.icio.us/v1/posts/recent"); static void Main(string[] args) { HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri); request.Credentials = new NetworkCredential(user, password); HttpWebResponse response = (HttpWebResponse) request.GetResponse(); XPathDocument xml = new XPathDocument(response.GetResponseStream()); XPathNavigator navigator = xml.CreateNavigator(); foreach (XPathNavigator node in navigator.Select("/posts/post")) { string description = node.GetAttribute("description",""); string href = node.GetAttribute("href",""); Console.WriteLine(description + ": " + href); } } }
5.PHP
libcurl
<?php $user = "username"; $password = "password"; $request = curl_init(); curl_setopt($request, CURLOPT_URL, 'https://api.del.icio.us/v1/posts/recent'); curl_setopt($request, CURLOPT_USERPWD, "$user:$password"); curl_setopt($request, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($request); $xml = simplexml_load_string($response); curl_close($request); foreach ($xml->post as $post) { print "$post[description]: $post[href]/n"; } ?>
6.Javascript
XMLHttpRequest
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/transitional.dtd"> <!--delicious-ajax.html--> <!--An Ajax application that uses the del.icio.us web service. This application will probably only work from a local file. Even then, your browser's security policy might prevent it from running.--> <html> <head><title>Javascript del.icio.us</title></head> <body> <h1>Javascript del.icio.us example</h1> <p>Enter your del.icio.us account information, and I'll fetch and display your most recent bookmarks.</p> <form onsubmit="callDelicious(); return false;"> Username: <input id="username" type="text" /><br /> Password: <input id="password" type="password" /><br /> <input type="submit" value="Fetch del.icio.us bookmarks"/> </form> <div id="message"> </div> <ul id="links"></ul> <script type="text/javascript"><!-- function setMessage(newValue) { message = document.getElementById("message"); message.firstChild.textContent = newValue; } function callDelicious() { try { if (netscape.security.PrivilegeManager.enablePrivilege) { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); } } catch (e) { alert("Sorry, browser security settings won't let this program run."); return; } var username = document.getElementById("username").value; var password = document.getElementById("password").value; // Remove any old links var links = document.getElementById("links"); while (links.firstChild) { links.removeChild(links.firstChild); } setMessage("Please wait..."); request = new XMLHttpRequest(); request.open("GET", "https://api.del.icio.us/v1/posts/recent", true, username, password); request.onreadystatechange = populateLinkList; request.send(null); // Called when the HTTP request has completed. function populateLinkList() { if (request.readyState != 4) // Request has not yet completed { return; } setMessage("Request complete."); if (netscape.security.PrivilegeManager.enablePrivilege) { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); } alert(request.responseText); posts = request.responseXML.getElementsByTagName("post"); setMessage(posts.length + " link(s) found:"); for (var i = 0; i < posts.length; i++) { post = posts[i]; var link = document.createElement("a"); var description = post.getAttribute('description'); link.setAttribute("href", post.getAttribute('href')); link.appendChild(document.createTextNode(description)); var listItem = document.createElement("li"); listItem.appendChild(link); links.appendChild(listItem) } } } // --></script> </body> </html>
B.XML解析
1.ruby
REXML
2.Python
ElementTree
3.Java
javax.xml Xerces XMLPull
4.C#
System.Xml.XmlReader
5.PHP
libxml2
6.Javascript
responseXML
-------------------------------------------------------
Java Framework: http://www.restlet.org/