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);

[3609 byte] By [rcauvina] at [2007-11-27 9:05:40]
# 1

GETs and POSTs are not interchangeable. And ASP is a good example of a case where it isn't.

<%

' fetches value of fs from querystring aka GET

aVal = Request.QueryString("fs")

' fetches value of fs from form aka POST

aVal = Request.Form("fs")

%>

anyway just use the get if it works. I don't see what the problem is.

cotton.ma at 2007-7-12 21:40:10 > top of Java-index,Core,Core APIs...
# 2
As I already said, I know for a fact that another program (not written in Java) is successfully using a POST and passing the same encoded parameters.
rcauvina at 2007-7-12 21:40:10 > top of Java-index,Core,Core APIs...
# 3
String content = "fs=714+Montgomery+St\\n\\r";String content = "fs=714+Montgomery+St\\r\\n";
BIJ001a at 2007-7-12 21:40:10 > top of Java-index,Core,Core APIs...
# 4
> > String content = "fs=714+Montgomery+St\\n\\r";> String content = "fs=714+Montgomery+St\\r\\n";> Thanks, but reversing the order of the '\r' and '\n' makes no difference.
rcauvina at 2007-7-12 21:40:10 > top of Java-index,Core,Core APIs...
# 5

My apologies.

It turns out that the other program returning the "correct" results is using GET. It was using forms, hidden input fields, and submits, but it wasn't specifying POST as the method, so it defaulted to GET. I just tweaked the other program to use POST, and now it fails as well.

So I will do as cottom.m suggested and use GET. I didn't want to use GET, because I'm using a framework that already uses POST. Unfortunately, I'm going to have to customize the framework.

rcauvina at 2007-7-12 21:40:10 > top of Java-index,Core,Core APIs...
# 6
> Thanks, but reversing the order of the '\r' and '\n'> makes no difference.FYI it is always \r\n, never the other way around.
ejpa at 2007-7-12 21:40:10 > top of Java-index,Core,Core APIs...