HTTP GET Requests
An HTTP GET request is all you need to retrieve basic search results from the Yahoo! Search Web Service. GET requests are easy to debug because you can paste it in your web browser and quickly see if the query has been constructed properly. Some web services require requests via POST for tasks such as image uploading or the modification of HTTP headers to set cookies. We will cover both GET and POST requests in the following examples.
The first example is a request for the keyword "umbrella" that returns 10 results:
We make a simple HTTP GET request to retrieve the results:
Next, we process the response from Yahoo! Web Services and print the XML response to the console:
A complete code example is available in the file YahooWebServiceGet.java
HTTP POST Requests
POST requests are used by web browsers when submitting data from HTML forms. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.
For POST requests, we construct the POST data separately from the request URL and parameters.
The response is the same as the first example and can be processed in the same way.
A complete code example is available in the file YahooWebServicePost.java
......................................................
....................................................
http://www.aviransplace.com/2008/01/08/make-http-post-or-get-request-from-java/
http://developer.yahoo.com/java/howto-reqRestJava.html#get
http://hc.apache.org/httpclient-3.x/tutorial.html
http://www.vogella.de/articles/ApacheHttpClient/article.html
......................................................
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLEncoder;
- public class POSTDATA {
- public POSTDATA() {
- }
- public static void main(String[] args) {
- try {
- // Construct data
- String data = URLEncoder.encode("fName", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
- data += "&" + URLEncoder.encode("lName", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
- // Send data
- URL url = new URL("http://localhost/TUT/POSTDATA/post.php");
- URLConnection conn = url.openConnection();
- conn.setDoOutput(true);
- OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
- wr.write(data);
- wr.flush();
- // Get the response
- BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = rd.readLine()) != null) {
- System.out.println(line);
- }
- wr.close();
- rd.close();
- } catch (Exception e) {
- }
- }
- }
....................................................
http://www.aviransplace.com/2008/01/08/make-http-post-or-get-request-from-java/
http://developer.yahoo.com/java/howto-reqRestJava.html#get
http://hc.apache.org/httpclient-3.x/tutorial.html
http://www.vogella.de/articles/ApacheHttpClient/article.html
No comments:
Post a Comment