Best web hosting site - 348 CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER
December 17th, 2007348 CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER PATTERN Using an HTTP Client Library to Execute a Query An HTTP request is not complicated to create, but there are some things that need to be accounted for. For example, when making a request by using the query Newest Applications, the query has to be URL-encoded to Newest+Applications. If you do not URL-encode the query string, problems will occur because the server will most likely be unable to process the request. Therefore, when making HTTP requests, use a client library that manages URL encodings, HTTP cookies, and anything associated with the technical details of using the HTTP protocol. The examples in this book use Java and the library HttpClient, which is an Apache Jakarta Commons library (http://Jakarta.apache.org). Following is the Java source code used to prepare the request: HttpClient client = new HttpClient(); HttpMethod method = new GetMethod( _endpoint); String timeStamp = Signature.generateTimestamp(); String signature; String operation = “WebSearch”; try { signature = Signature.generateSignature(operation, timeStamp, _secretAccessKey); } catch (SignatureException e) { return; } The variable client is the top-level variable used to make an HTTP request. The variable method defines the parameters of the HTTP request. The Amazon.com search request requires an HTTP GET, which is possible by instantiating the GetMethod class. The signature and operation variables contain the values used to define the associated URL variables. The variable _endpoint contains a value from the configuration file that is the HTTP URL used to call the Amazon.com web service. The method Signature.generateSignature converts the configuration-defined Amazon.com secret access key (_secretAccessKey) into a hash-encoded value. To call the Amazon.com REST web service, the variables are assembled and form an HTTP request: NameValuePair[] items = new NameValuePair[] { new NameValuePair( “Service”, “AlexaWebInfoService”), new NameValuePair( “Operation”, operation), new NameValuePair( “AWSAccessKeyId”, _accessKey), new NameValuePair( “Signature”, signature), new NameValuePair( “Timestamp”, timeStamp), new NameValuePair( “ResponseGroup”, “Results”), new NameValuePair( “Query”, _request.getQueryString()) }; method.setQueryString( items); try { client.executeMethod(method); if( method.getStatusCode() == 200) { processResults( _request.getQueryIdentifier(), method.getResponseBodyAsStream()); }
In case you need quality webspace to host and run your web applications, try our personal web hosting services.