Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Posting with HttpClient

Status
Not open for further replies.

roshik

Programmer
Jun 14, 2005
3
0
0
GB
Please could someone help me. Why does the following code not work? It keeps reloading the same page instead of going on to the next page.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;

public class PostTest {
public static void main( String[] args ) {

String url = " try {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod( url );

method.setDoAuthentication(true);

// Configure the form parameters
method.addParameter( "login", "myuserid");
method.addParameter( "passwd", "mypassword" );

// Execute the POST method
int statusCode = client.executeMethod( method );
if( statusCode != -1 ) {
String contents = method.getResponseBodyAsString();

File name = new Fil("C:/project/test.html");
FileWriter fw = new FileWriter(name);

try {
fw.write(contents);
} catch (IOException e) {
}
method.releaseConnection();
fw.close();
}
}
catch( Exception e ) {
e.printStackTrace();
}
}
}
 
stefanwagner,
I'm sorry, are u telling me that I am not printing out what is in the exception u have pointed out?
Well even when i do printStackTrace(), there is no exception.

The message I get is:

18-Jun-2005 02:09:29 org.apache.commons.httpclient.HttpMethodBase getResponseBody
WARNING: Going to buffer response body of large or unknown size. Using getResponseAsStream instead is recommended.

I've tried using getResponseAsStream as well but the same login page is loaded.
 
Your code only makes call to one page. It won't go the next page unless you make another call, right?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top