Hi,
I am trying to make multiple posts over a https channel (basically fill out
an interview process).The first post works fine and the desired response is
obtained from the server, however the second post is causing an internal
Error on the remote server, assuming that the post data and headers are set
correctly is there anything missing in the code (I have used this earlier on
a http connection and it worked fine, so I am wondering if some extra
configuration is required for Https connection)??
Code-------
*import* java.io.IOException;
*import* java.util.ArrayList;
*import* java.util.List;
*import* org.apache.http.NameValuePair;
*import* org.apache.http.client.ResponseHandler;
*import* org.apache.http.client.entity.UrlEncodedFormEntity;
*import* org.apache.http.client.methods.HttpPost;
*import* org.apache.http.cookie.Cookie;
*import* org.apache.http.impl.client.BasicResponseHandler;
*import* org.apache.http.impl.client.DefaultHttpClient;
*import* org.apache.http.message.BasicNameValuePair;
*import* org.apache.http.protocol.HTTP;
*import* org.apache.log4j.BasicConfigurator;
*import* org.apache.log4j.Logger;
*public* *class* DirectPost {
*protected* *final* Logger logger = Logger.*getLogger*(DirectPost.*
class*);
*public* *static* *void* main(String args[]) {
DirectPost postToRemoteServer = *new* DirectPost();
postToRemoteServer.postData();
}
*public* *void* postData() {
String uri;
DefaultHttpClient client;
HttpPost postMethod = *null*;
String sessionCookie = *null*;
*try* {
BasicConfigurator.*configure*();
// The first URI to connect to
uri = "First url goes here";
// Instantiate HttpClient
client = *new* DefaultHttpClient();
// Instantiate the post method
postMethod = *new* HttpPost(uri);
ResponseHandler<String> responseHandler =
*new*BasicResponseHandler();
// Set up all the required request headers
initializeConnectionHeaders(postMethod);
// Execute the first post
String firstResponse = client.execute(postMethod,
responseHandler);
logger.info(firstResponse);
// Second post
HttpPost httpost = *new* HttpPost(
"second url goes here");
List<NameValuePair> nvps = *new*ArrayList<NameValuePair>();
nvps.add(*new* BasicNameValuePair("acceptButton", "I
Agree"));
httpost.setEntity(*new* UrlEncodedFormEntity(nvps, HTTP.*
UTF_8*));
httpost.setHeader("Referer",
"Referer url goes here");
initializeConnectionHeaders(httpost);
firstResponse = client.execute(httpost, responseHandler);
logger.info(firstResponse);
//This response indicates an error at the remote server
} *catch* (IOException e) {
logger.error("Post to remote server Failed caused by" +
e);
}
}
/**
* This method sets up all the required request headers
*
* [EMAIL PROTECTED] method
*/
*private* *void* initializeConnectionHeaders(HttpPost method) {
method.setHeader("Content-Type",
"application/x-www-form-urlencoded");
method
.setHeader(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT
5.1; en-US; rv:1.9.0.3) Gecko/2008092417
Firefox/3.0.3");
method.setHeader("Connection", "keep-alive");
method.setHeader("Keep-Alive", "300");
method
.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
method.setHeader("Accept-Language", "en-us,en;q=0.5");
method.setHeader("Accept-Encoding", "gzip,deflate");
method.setHeader("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
}
}
With warm regards,
Vishwas