HTTP Post Returning Different Results from HTTP Get
I am having trouble with an HTTP POST returning different results from
an equivalent HTTP GET. If you go to the following URL in a browser:
http://transit.511.org/tripplanner/itinresults.asp&fs=714+Montgomery+St
you will see that it displays a web page containing the following text:
"We found some location(s) similar to your origin entry, but no exact match."
I get that same result from my Java program if I use a GET request.
However, if I use an equivalent POST from my Java program, I do not
get the same result. Instead, I get a web page that contains (among
other content) the following text:
"Please enter a STARTING LOCATION to process your itinerary request."
I know for a fact that other programs (to which I do not have access, and
which are probably not written in Java) are successfully using a POST,
but mine isn't working. Why not?
Here is my code:
String actionURLString ="http://transit.511.org/tripplanner/itinresults.asp";
String content ="fs=714+Montgomery+St\\n\\r";
int length = content.length();
String contentLength = Integer.toString(length);
URL url =new URL(actionURLString);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length", contentLength);
httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (compatible; Hello; http://www.hello.com)");
OutputStream outputStream = httpURLConnection.getOutputStream();
OutputStreamWriter writer =new OutputStreamWriter(outputStream);
writer.write(content);
writer.flush();
writer.close();
int responseCode = httpURLConnection.getResponseCode();
InputStream inputStream = httpURLConnection.getInputStream();
Reader inputStreamReader =new InputStreamReader(inputStream);
Reader reader =new BufferedReader(inputStreamReader);
StringBuffer stringBuffer =new StringBuffer();
int numCharactersRead;
do
{
char characters[] =newchar[1024];
numCharactersRead = reader.read(characters, 0, 1024);
if (numCharactersRead > 0)
stringBuffer.append(characters, 0, numCharactersRead);
}
while (numCharactersRead != -1);
String response = stringBuffer.toString();
reader.close();
inputStreamReader.close();
System.out.println(response);

