I need to login to an IIS web-server.
After login, I need to send an HTTP Post with a lot of data.
However, I don't know how to save my cookies from login and reuse them for subsequent HTTP calls.
My code
private final static Logger logger = Logger.getLogger(HttpURLConnectionExample.class);
private static final String CERTIFICATE_CLIENT = "/src/main/resources/clientcert.jks";
private static final String JAVAX_NET_SSL_KEY_STORE_PASSWORD = "javax.net.ssl.keyStorePassword";
private static final String JAVAX_NET_SSL_KEY_STORE = "javax.net.ssl.keyStore";
private static PropertiesManagement management;
private static final String LOGIN_PAGE = "https://alloggiatiweb.poliziadistato.it/Alloggiatiweb/Login.aspx";
private static final String COOKIES_HEADER = "Set-Cookie";
private static CookieManager msCookieManager = new CookieManager();
private static final String INTERNAL_PAGE = "https://alloggiatiweb.poliziadistato.it/Alloggiatiweb/Analisi.aspx";
private HttpsURLConnection conn;
private static void setCertificateSSL() throws FileNotFoundException, KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
FileInputStream fin = new FileInputStream(management.getFilePath());
KeyStore ks = KeyStore.getInstance(management.getSSLProtocol());
ks.load(fin, management.getPassword().toCharArray());
System.setProperty(JAVAX_NET_SSL_KEY_STORE, CERTIFICATE_CLIENT);
System.setProperty(JAVAX_NET_SSL_KEY_STORE_PASSWORD, management.getPassword());
}
public static void main(String[] args) throws Exception {
management = new PropertiesManagement();
setCertificateSSL();
HttpURLConnectionExample http = new HttpURLConnectionExample();
http.login();
}
private void getInternalPage() throws Exception {
URL obj = new URL(INTERNAL_PAGE);
conn = (HttpsURLConnection) obj.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
logger.info("[GET ON " + obj + " RESPONSE CODE-MESSAGE]" + conn.getResponseCode() + " - " + conn.getResponseMessage());
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
logger.info(response.toString());
}
private void login() throws Exception {
URL obj = new URL(LOGIN_PAGE);
conn = (HttpsURLConnection) obj.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("txtUtente", management.getUsername()));
params.add(new BasicNameValuePair("txtPwd", management.getPassword()));
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();
Map<String, List<String>> headerFields = conn.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
if (cookiesHeader != null) {
for (String cookie : cookiesHeader) {
msCookieManager.getCookieStore().add(null, HttpCookie.parse(cookie).get(0));
}
}
logger.info("[POST ON " + obj + " RESPONSE CODE-MESSAGE]" + conn.getResponseCode() + " - " + conn.getResponseMessage());
getInternalPage();
conn.disconnect();
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
and this is response
INFO (HttpURLConnectionExample.java:125) [19 apr 2016 15:08:51,013] - [POST ON https://alloggiatiweb.poliziadistato.it/Alloggiatiweb/Login.aspx RESPONSE CODE-MESSAGE]200 - OK
INFO (HttpURLConnectionExample.java:83) [19 apr 2016 15:08:51,461] - [GET ON https://alloggiatiweb.poliziadistato.it/Alloggiatiweb/Analisi.aspx RESPONSE CODE-MESSAGE]200 - OK
INFO (HttpURLConnectionExample.java:94) [19 apr 2016 15:08:51,464] - <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transit
.....
but the page print aren't internal page but it's login page