Java Reference
In-Depth Information
NOTE
One minor adjustment that you'll have to make only when posting is to set the HTTP Content-
Type header to application/atom+xml .
You also see the standard use of RESTful URIs at work in this example. The typical way to
create URIs uses a “container” pattern to drill down from general to specific resources. For
example, in the following URI, you drill down through the finance container to the feeds, then
to “default” (which represents the current user in this case), then to that user's portfolios, and
then to the specific portfolio with the ID of 1:
http://finance.google.com/finance/feeds/default/portfolios/1
Without further ado, let's get into the example.
Creating the Client
A Google Finance Portfolio is a collection of stocks or mutual funds that you want to watch.
This client will interact with the RESTful API to create a new portfolio. You could then use
additional aspects of the API to add or remove stocks to update the portfolio.
Example 8-5 shows the complete Java program that runs the show. This will authorize you
with your Google account, create a new Finance Portfolio in your account called “My Restful
Portfolio,” and then retrieve a list of all the portfolios associated with your account (including
your new one).
Example8-5.GoogleFinanceRestClient.java
package simplerestclient;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URLEncoder;
import java.security.Security;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.SSLSocket;
/**
* Google finance API client authenticates over SSL, then
Search WWH ::




Custom Search