public static void main(String[] args)
throws Exception {
final WebRequestCodeSample webRequestCodeSample =
new WebRequestCodeSample();
final String authenticationTicket =
webRequestCodeSample.getAuthenticationTicket("username",
"password");
webRequestCodeSample
.getData(
new URL( "http://technet.rapaport.com/HTTP/DLS/GetFile.aspx"),
authenticationTicket);
}
private String getAuthenticationTicket(
final
String username,
final String password)
throws IOException {
final URL url =
new
URL(
"https://technet.rapaport.com/HTTP/Authenticate.aspx");
final String data = URLEncoder.encode(
"username",
"UTF-8").concat(
"=")
.concat(URLEncoder.encode(username, "UTF-8")).concat("&")
.concat(URLEncoder.encode("password",
"UTF-8")).concat("=")
.concat(URLEncoder.encode(password, "UTF-8"));
final URLConnection connection = url.openConnection();
connection.setDoOutput(
true);
final OutputStream outputStream = connection.getOutputStream();
final OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(outputStream);
outputStreamWriter.write(data);
outputStreamWriter.flush();
final BufferedReader rd =
new
BufferedReader(
new InputStreamReader(connection.getInputStream()));
final StringBuffer stringBuffer =
new StringBuffer();
String line;
while ((line = rd.readLine()) !=
null) {
stringBuffer.append(line);
}
outputStreamWriter.close();
rd.close();
return stringBuffer.toString();
}
private void getData(
final
URL url,
final String ticket)
throws IOException {
final String data = URLEncoder.encode(
"ticket",
"UTF-8").concat(
"=").concat(URLEncoder.encode(ticket,
"UTF-8"));
final URLConnection connection = url.openConnection();
connection.setDoOutput(
true);
connection.setRequestProperty(
"Accept-Encoding",
"gzip");
final OutputStream outputStream = connection.getOutputStream();
final OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(outputStream);
outputStreamWriter.write(data);
outputStreamWriter.flush();
final InputStream replyStream = connection.getInputStream();
final InputStreamReader inputStreamReader;
final String contentEncodingField = connection.getHeaderField(
"Content-Encoding");
if (contentEncodingField !=
null
&& contentEncodingField.equalsIgnoreCase(
"gzip"))
{
final GZIPInputStream gzipInputStream =
new GZIPInputStream(replyStream);
inputStreamReader = new InputStreamReader(gzipInputStream);
}
else {
inputStreamReader = new InputStreamReader(replyStream);
}
final BufferedReader reader =
new BufferedReader(inputStreamReader);
String line;
while ((line = reader.readLine()) !=
null) {
System.out.println(line);
}
outputStreamWriter.close();
reader.close();
}
}