1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
String targetURL = "https://dddddddd";
String urlParameters =URLEncoder.encode("j_username","UTF-8")+"="+URLEncoder.encode(login, "UTF-8")+"&"+URLEncoder.encode("j_password", "UTF-8")+"="+URLEncoder.encode(pwd, "UTF-8")+"&"+URLEncoder.encode("rememberMe", "UTF-8")+"="+URLEncoder.encode("on", "UTF-8")+"&"+URLEncoder.encode("langCode", "UTF-8")+"="+URLEncoder.encode("fra", "UTF-8")+"&"+URLEncoder.encode("lang", "UTF-8")+"="+URLEncoder.encode("fra", "UTF-8"));
/ System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
// java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
String cuki=new String();
URL url;
System.out.println("urlParameters : "+urlParameters);
HttpURLConnection connection = null;
try {
//Create connection
// SSLContext sslctx = SSLContext.getInstance("SSL");
// sslctx.init(null, new X509TrustManager[] { new MyTrustManager() }, null);
// HttpURLConnection.setDefaultSSLSocketFactory(sslctx.getSocketFactory());
//HttpsURLConnection.setDefaultHostnameVerifier(newMyHostnameVerifier());
// Authenticator.setDefault(new MyAuthenticator());
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
// String cookieHeader = connection.getHeaderField("set-cookie");
// if(cookieHeader != null) {
// int index = cookieHeader.indexOf(";");
// if(index >= 0)
// cuki = cookieHeader.substring(0, index);
// connection.setRequestProperty("Cookie", cuki);
// }
// connection.setHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier());
// if (connection instanceof HttpsURLConnection) {
// try {
// KeyManager[] km = null;
// TrustManager[] tm = {new RelaxedX509TrustManager()};
// SSLContext sslContext = SSLContext.getInstance("SSL");
// sslContext.init(null, tm, new java.security.SecureRandom());
// SSLSocketFactory sf = sslContext.getSocketFactory();
// ((HttpsURLConnection)connection).setSSLSocketFactory(sf);
// }
// catch (java.security.GeneralSecurityException e) { throw new IOException(e.getMessage()); }
// }
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Connection" ,"keep-alive");
// connection.setRequestProperty("Server" ,"Oracle-Application-Server-10g");
connection.setRequestProperty("Host", "xxxxx");
connection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
connection.setRequestProperty("Accept-Encoding","gzip,deflate");
connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB7.1 ( .NET CLR 3.5.30729)");
connection.setRequestProperty("REFERER","xxxxxxx");
connection.setRequestProperty("keep-alive", "timeout=15, max=100");
connection.setRequestProperty("Cookie","xxxx");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
// connection.setRequestProperty("Content-Language", "en-US");
connection.setAllowUserInteraction(false);
connection.setUseCaches (false);
connection.setRequestMethod("POST");
connection.setInstanceFollowRedirects(true);
connection.connect();
//Send request
OutputStreamWriter wr = new OutputStreamWriter (
connection.getOutputStream ());
wr.write(urlParameters);
wr.flush ();
readCookies( connection );
if(connection.getResponseCode() == 200) {
System.out.println("Worked");
} else {
System.out.println("Didn't work");
}
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
wr.close ();
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
} |
Partager