Google Answers Logo
View Question
 
Q: POST method in java ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: POST method in java
Category: Computers > Programming
Asked by: hamma-ga
List Price: $60.00
Posted: 27 Dec 2003 16:28 PST
Expires: 26 Jan 2004 16:28 PST
Question ID: 290759
I want to be able to request information about a postal code for a
certain adress from this site:
http://www.postescanada.ca/tools/pcl/bin/advanced-e.asp
I want a java code with some examples.

Clarification of Question by hamma-ga on 27 Dec 2003 16:38 PST
Actually I need the code that do that, not sites that explain how to do it.

Request for Question Clarification by theta-ga on 27 Dec 2003 16:56 PST
Hi hamma-ga,
   While I am interested in answering your question, I am afraid it
will require a much higher investment of time and effort than is
associated with the price you have offered.
   I suggest you take a look at the Google Answers Pricing Guideline
and revise your offer appropriately.
     - Google Answers: How to price your question
       (http://answer.google.com/answers/pricing.html)

Regards,
Theta-ga

Clarification of Question by hamma-ga on 27 Dec 2003 17:00 PST
Ok :) I changed the price.

Clarification of Question by hamma-ga on 27 Dec 2003 17:13 PST
Please I need the code today, it's very important.
Thank you.
Theta, are you still working on it???

Request for Question Clarification by answerguru-ga on 28 Dec 2003 00:25 PST
Hi hamma-ga,

I noticed that nobody has answered this for you even though you have
stated the urgency of getting an answer. I took a look to see if any
examples were readily available but have not been able to come across
any. I can write up one for you myself, however this takes at least a
few hours and your current price is not appropriate for that level of
work. If you are still interested in an answer I would be able to have
something for you by tomorrow afternoon (assuming a reasonable price
is offered).

Thanks,
answerguru-ga

Request for Question Clarification by theta-ga on 28 Dec 2003 03:00 PST
Hi hamma-ga,
   Sorry i didn't reply earlier, but I was offline for the last few
hours. I notice that you have set a deadline of today for this
question. I am afraid meeting that deadline would not be possible, but
if you can extend the time limit, I am still interested in answering
the question.
    Also, researchers trying to answer this question will benefit from
any added details you can provide us regarding the Java program
required. For example, I took a look at the site mentioned in your
question and noted that the result of our queries could be three
possible types: a single address, a list of addresses(could extend to
multiple pages) and a 'not found' error.
 - I assume you just want the program to display the returned result
in the same tabular form.
 - In case of multiple returned results, do you want the program to
display all the results in all the pages , or just results on the
first page? Keep in mind that due to the formatting applied,
retrieving all the result pages could be a complicated task.
 - I assume that you will be satisfied with a Java console program
that takes in the required search fields and displays the returned
address?
   Thirdly, I appreciate the increase in the question's price, but as
answerguru-ga also pointed out, it is still not proportional to the
time and effort required for this question. Please revise either the
price or the scope of this question appropriately.

Regards,
Theta-ga

Clarification of Question by hamma-ga on 28 Dec 2003 09:23 PST
Time extended.
Now I understand why you said that the price is not proportional.
Actually I need only a few lines of code that do the post to the site
and retrieve the page that is returned, I dont need any formatting,
just the html page returned.
I dont need any frame, I need only the html page printed on the consol
as it is returned by the server, whatever answer the server returnrs.

Clarification of Question by hamma-ga on 28 Dec 2003 11:37 PST
Theta, are you working on it?

Request for Question Clarification by theta-ga on 28 Dec 2003 17:23 PST
Hi hamma-ga,
  I'm on it  :-)

Regards,
Theta-ga

Clarification of Question by hamma-ga on 28 Dec 2003 18:21 PST
Thanks in advance.

Clarification of Question by hamma-ga on 28 Dec 2003 19:19 PST
Theta, please could you tell me if you feel you are in the good way to
find the answer?
Im planning to cancel the question in about 2 hours from now if it
doesnt get an answer, because tomorrow it will be too late.
Thank you.

Request for Question Clarification by googleexpert-ga on 28 Dec 2003 19:39 PST
Hi hamma, 
Are you looking for Applet code or Servlet Code?

Clarification of Question by hamma-ga on 28 Dec 2003 19:50 PST
just a few lines of code, Im planning to include them in my
application. it's an application.

Clarification of Question by hamma-ga on 28 Dec 2003 20:18 PST
Is there someone working on my question?
Im gonna delete it in one hour if i get no answer.

Request for Question Clarification by theta-ga on 28 Dec 2003 20:59 PST
Hi hamma-ga,
   I have been working on your question, and have completed the code
required to connect to the website, retrieve the results page and dump
it to the console. However, the code for allowing the user to enter
data for the search fields is still left.
   If you want, I can post the connect code right now, and then the
User input code in a couple of hours.
Regards,
Theta-ga
:-)

Clarification of Question by hamma-ga on 28 Dec 2003 21:03 PST
Good it's more than enogh, just shoot it, of course if you are sure that it works.

Clarification of Question by hamma-ga on 28 Dec 2003 21:32 PST
Theta, send what you have I can arrange the details by myself.

Clarification of Question by hamma-ga on 28 Dec 2003 21:52 PST
are you having difficulties Theta???

Clarification of Question by hamma-ga on 28 Dec 2003 22:07 PST
Theta, I need this piece of code to finish a work for tomorrow
morning. Please respond and tell me when you plan to post an answer. I
have to delete the question tonight as I dont need it anymore if I
dont get an answer in few minutes. Please give a sign or unlock the
answer.
Thank you for your understanding.
Answer  
Subject: Re: POST method in java
Answered By: theta-ga on 28 Dec 2003 22:16 PST
Rated:5 out of 5 stars
 
Hi hamma-ga,
   No difficulties, just formatting and testing the code to see ensure
that it works right :-)
 Here is the Java code for a program that connects to the search page
and retrieves the results.

============================ Post.java ======================================


import java.net.*;
import java.io.*;

public class Post {

 public static void main(String[] args) {
   try
   {	
     // The URL of the result asp page
     String resultURL =
"http://www.postescanada.ca/tools/pcl/bin/cp_search_response-e.asp";
			
     // The following variables store the contents of the 10 search fields
     // By default all are empty
     // Make sure at least two address fields are filled!
     String street_number="";
     String suffix="";
     String unit_number="";
     String street_name="";
     String street_type="";
     String street_direction="";
     String city="";
     String prov="";
     String delivery_mode="";
     String delivery_number="";
			
     // Connect to the ASP page
     URL postURL = new URL(resultURL);     
     System.out.println(">>> Trying to Connect...");
     HttpURLConnection con = (HttpURLConnection) postURL.openConnection();
     
     // Set properties for the HTTP connection
     con.setUseCaches(false);       // do not use cache
     con.setDoOutput(true);         // use for output
     con.setDoInput(true);          // use for Input
     con.setRequestMethod("POST");  // use the POST method to submit the form
            
     //Get the outputStream so we can send the query 
     System.out.println(">>> Getting the OutputStream...");
     PrintWriter out = new PrintWriter(con.getOutputStream());
     
     //Create the query string string 
     
     // Add the hidden form fields. These values are always constant
     String postStr;
     postStr ="app_response_start_row_number="+URLEncoder.encode("1")+"&"+	
             "app_response_rows_max="+URLEncoder.encode("9")+"&"+	
     		 "app_language="+URLEncoder.encode("english")+"&"+
	     	 "app_source="+URLEncoder.encode("advanced")+"&"+	
     		 "query_source="+URLEncoder.encode("a")+"&"+	
     		 "app_state="+URLEncoder.encode("production");	
	
     // Add the search fields to the query string           		
     postStr += "street_number="+URLEncoder.encode(street_number) +"&"+
         "Suffix="+URLEncoder.encode(suffix) +"&"+
         "unit_number="+URLEncoder.encode(unit_number) +"&"+
         "street_name="+URLEncoder.encode(street_name) +"&"+
         "street_type="+URLEncoder.encode(street_type) +"&"+
         "street_direction="+URLEncoder.encode(street_direction) +"&"+
         "city="+URLEncoder.encode(city) +"&" +
         "prov="+URLEncoder.encode(prov)+"&" +
         "delivery_mode="+URLEncoder.encode(delivery_mode) +"&" +
         "delivery_number="+URLEncoder.encode(delivery_number) ;
     
     // POST the query string to the server
     System.out.println(">>> POSTING: "+postStr);
     out.println(postStr); // send to server
     out.close();   // close outputstream
     
     
     //Get the results page from the server
     String inputLine="";   //Stores the line of text returned by the server
     String resultsPage=""; // Stores the complete HTML results page
     
     System.out.println(">>> Waiting for response...\n\n");
     BufferedReader in = new BufferedReader(
     		     new InputStreamReader(con.getInputStream()));          		  

     while ((inputLine = in.readLine()) != null)
     	   resultsPage+=inputLine;
     in.close();
     
     // Dump the results page onto the console
     // resultsPage contains the entire HTML result page
     // You can write it to a file or display on the screen
     System.out.println(resultsPage);
     
	}catch(Exception e){
     // catch any exceptions
     System.out.println(e);
	}//end catch
  } // end main
} // end class

==========================================================================

Explaination follows.  :-)

Regards,
Theta-ga
:-)

Request for Answer Clarification by hamma-ga on 28 Dec 2003 22:21 PST
Thank you, give me few minutes to test.

Request for Answer Clarification by hamma-ga on 28 Dec 2003 22:36 PST
It works like a charm. Thank you very much

Clarification of Answer by theta-ga on 28 Dec 2003 22:53 PST
Hi hamma-ga,
    A brief description of the query string that is posted to the
server. This query string is made up of the search fields provided on
the search page and some hidden fields contained in the form.
  The search fields provided by the user are stored in the 10 string
variables declared in the beginning of the main method. Remember that
atleast 2 of the address fields should be filled, otherwise you will
not get any matches. By default, all the fields are empty(""). A
description of the 10 variables and their allowed values follows:
  1. street_number : Corresponds to the 'Street Number' search field.
  2. suffix : Corresponds to the 'Number Suffix' search field. Valid
values are NULL, 1/4,1/2,3/4, and alphabets from A to Z.
  3. unit_number : Corresponds to the 'Unit/Suite/Apt' search field.
  4. street_name : Corresponds to the 'Street Name' search field.
  5. street_type : Corresponds to the 'Street Type' search field.
  6. street_direction : Corresponds to the 'Street Direction' search
field. Valid values are NULL, E, N, NE, NW, S, SE, SW and W.
  7. city : Corresponds to the 'Municipality (City, Town, etc.)' search field.
  8. prov : Corresponds to the 'Province' search field. Valid values
are NULL, AB, BC, MB, NB, NL, NS, NT, NU, ON, PE, QC, SK, and YT.
  9. delivery_mode : Corresponds to the 'Delivery Mode' search field.
valid values are NULL,GD, MR, PO, RR, and SS.
 10. delivery_number : Corresponds to the 'PO Box or Route Number' search field.

The hidden fields included in the query string are there just for the
sake of completeness. The application works fine even if they are not
included.

======================
Hope this helps.
If you need any clarifications, just ask!

Regards,
Theta-ga
:-)

BTW, thanks for the kind words and the great rating!
hamma-ga rated this answer:5 out of 5 stars
it seem this person is a perfectionist.
very friendly and professional.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy