Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

How to open http connection using j2me

Wed Jul 25, 2007 5:00 pm

How to open http connection using j2me, Open an http connection ..
Code:
public class Fetcher {

    private static final String BASE_URL = "http://www.amazon.com";
    private static final String QUERY_URL = BASE_URL +
                                "/exec/obidos/search-handle-form/0";

    // Fetches the title, ranking and review count
    // for a book with a given ISBN.
    public static boolean fetch(BookInfo info) throws IOException {

        InputStream is = null;
        OutputStream os = null;
        HttpConnection conn = null;
        int redirects = 0;
        try {
            String isbn = info.getIsbn();
            String query = "index=books&field-keywords=" + isbn + "\r\n";
            String requestMethod = HttpConnection.POST;
            String name = QUERY_URL;

            conn = (HttpConnection)Connector.open(name,
                                                Connector.READ_WRITE);

            // Send the ISBN number to perform the query
            conn.setRequestMethod(requestMethod);
            conn.setRequestProperty("Content-Type",
                        "application/x-www-form-urlencoded");
            os = conn.openOutputStream();
            os.write(query.getBytes());
            os.close();

            // Read the response from the server
            is = conn.openInputStream();
            int code = conn.getResponseCode();

            // Process the returned data (not shown here)
       } finally {
            // Tidy up (code not shown)
       }
    }
}
 

I hop it is useful
You will find more here
http://www.onjava.com/pub/a/onjava/2002/04/17/j2me.html



Post a reply
  Related Posts  to : How to open http connection using j2me
 Using java in http connection     -  
 HTTP POST msg connection close problem     -  
 Send parameters using HTTP GET     -  
 HTTP Server in Java     -  
 http proxy code     -  
 Get value from HTTP POST VARS     -  
 Build distribution applications by remoting(TCP/HTTP)     -  
 Multipart HTTP forms submitter - With Progress Information     -  
 Open in a new window help     -  
 Which open-source CMS is the best?     -  

Topic Tags

Java J2ME