Skip to main content

Http Post and Get Method

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
sssk
Offline
Joined: 2011-09-08

Hi All,

New to here. Need some help for a http post method to an authenticated site and afterwhich calls a get method for retrieving of data.

Below are the 2 APIs.

APIs

1)http://ipaddress/platform/Login?user=test&password=test

2)http://ipaddress/platform/reportdata

The first API using POST method and the second API using GET method. The second API should returns me XML data. But it returns me saiding that I am not authenticated. So Is there any parts that I miss out or had I make any mistakes?? or how can I pass my session cookies to the GET request.

Greatly appreciate and please advice again. Thanks.

public class PostTestExample {

  public static void main(String args[]) {
  String POST_CONTENT_TYPE = "application/x-www-form-urlencoded";
  BufferedReader br = null;
  String LOGIN_USER_NAME = "test";
  String LOGIN_PASSWORD = "test";

  String LOGIN_USER_NAME_PARAMETER_NAME = "user";
  String LOGIN_PASSWORD_PARAMETER_NAME = "password";
  String TARGET_URL = "http://ipaddress/platform/Login";
       
    DataOutputStream dataOutputStream = null;
   
   
try {
String encodedLoginUserName = URLEncoder.encode(LOGIN_USER_NAME, "UTF-8");
    String encodedLoginPassword = URLEncoder.encode(LOGIN_PASSWORD, "UTF-8");
    String content = LOGIN_USER_NAME_PARAMETER_NAME +"="
            + encodedLoginUserName + "&" + LOGIN_PASSWORD_PARAMETER_NAME + "=" + encodedLoginPassword;

HttpURLConnection connection = (HttpURLConnection)(new URL(TARGET_URL).openConnection());        
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", POST_CONTENT_TYPE);
connection.setRequestMethod("POST");
   dataOutputStream = new DataOutputStream(connection.getOutputStream());

            // throws IOException
            dataOutputStream.writeBytes(content);
            dataOutputStream.flush();
            dataOutputStream.close();

BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;
   
    StringBuilder response = new StringBuilder();
    while ((line = rd.readLine()) != null) {
        // Process line...
    System.err.println(line);
    }

HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod("http://ipaddress/platform/reportdata");

  int returnCode = client.executeMethod(getMethod);
// FileWriter fw = new FileWriter ( "test.html" );
     //  BufferedWriter  bw = new BufferedWriter ( fw );

  if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
System.err.println("The Post method is not implemented by this URI");
getMethod.getResponseBodyAsString();

  }else{

String readLine;
br = new BufferedReader(new InputStreamReader(getMethod.getResponseBodyAsStream()));

  while(((readLine = br.readLine()) != null)) {
System.err.println(readLine);
//writeToFile(readLine);
//bw.write(readLine);
}
 
  br.close( );
  }
 
  rd.close();
  connection.disconnect();

}
catch( IOException e ){
// handle the error here
e.printStackTrace();
}
  }
  
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zurajava
Offline
Joined: 2011-09-30

can you test this url, any tools, exampl is firefox poster.

netx you look errors and you help google:D