400--Bad Request response
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ross_Cooksey
Posted On:   Thursday, June 14, 2001 02:03 PM

I have the following code: URL url = new URL(new String("http://localhost:7001/TestContent?fn=803.txt")); URLConnection connection = url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream instrm = connection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(instrm)); String msg = null; while ((msg = in.readLine()) != null) System.out.println(msg); All is well and good. The result is just plain text from the servlet indicating that the doGet method was executed. When the code is modified as follows: URL url = new URL(   More>>

I have the following code:

			
URL url = new URL(new String("http://localhost:7001/TestContent?fn=803.txt"));
URLConnection connection = url.openConnection();

connection.setDoInput(true);
connection.connect();

InputStream instrm = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(instrm));

String msg = null;
while ((msg = in.readLine()) != null)
System.out.println(msg);



All is well and good. The result is just plain text from the servlet indicating that the doGet method was executed.


When the code is modified as follows:

			
URL url = new URL(new String("http://localhost:7001/TestContent?fn=803.txt"));
HttpURLConnection connection = (HttpURLConnection)url.openConnection();

connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();

InputStream instrm = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(instrm));

String msg = null;
while ((msg = in.readLine()) != null)
System.out.println(msg);



things fall apart. I get back >Error 400--Bad Request <


In looking at the access.log file I see the following log statements (the GET works, the POST does not):


127.0.0.1 - - [14/Jun/2001:15:20:33 -0400] "GET /TestContent?fn=803.txt HTTP/1.0" 200 0



127.0.0.1 - - [14/Jun/2001:15:21:27 -0400] "POST /TestContent?fn=803.txt HTTP/1.0" 400 1296


Any thoughts? Thanks in advance.

   <<Less

Re: 400--Bad Request response

Posted By:   Ross_Cooksey  
Posted On:   Friday, June 15, 2001 05:38 AM

Found the answer. The following modifications were needed to get the POST working:


URL url = new URL(new String("http://localhost:7001/TestContent"));
String parms = new String("fn=803.txt");


connection.setDoOutput(true);
DataOutputStream dos = new DataOutputStream (connection.getOutputStream());
dos.writeBytes(parms);

About | Sitemap | Contact