SSLContext mySSLContext = SSLContext.getInstance("SSL");
mySSLContext.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(mySSLContext.getSocketFactory());
// Authenticate credentials
DefaultHttpClient httpclient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpGetID = new HttpGet(
httpclient.execute(httpGetID, localContext);
httpGetID.abort();
List<Cookie> cookies1 = cookieStore.getCookies();
for (Cookie cookie : cookies1) {
System.out.println("\t" + cookie.getName() + " : " + cookie.getValue());
}
//
List<NameValuePair> authFormParams = new ArrayList<NameValuePair>();
authFormParams.add(new BasicNameValuePair("j_username", "jtsadmin"));
authFormParams.add(new BasicNameValuePair("j_password", "avic123"));
// Listing 2. Build an UrlEncodedFormEntity
UrlEncodedFormEntity encodedentity = new UrlEncodedFormEntity(authFormParams,
"UTF-8");
HttpPost httpPostAuth = new HttpPost(
httpPostAuth.setEntity(encodedentity);
httpclient.execute(httpPostAuth, localContext);
List<Cookie> cookies2 = cookieStore.getCookies();
for (Cookie cookie : cookies2) {
System.out.println("\t" + cookie.getName() + " : " + cookie.getValue());
}
// Listing 3. Retrieve information about a work item
// READ BY ID
InputStream in=null;
OutputStream out = null;
httpclient = new DefaultHttpClient();
// // IMPORTANT STUFF
httpclient.setCookieStore(cookieStore);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entityPut = response.getEntity();
in=entityPut.getContent();
long length=entityPut.getContentLength();
if (length <= 0) {
System.out.println("下载文件不存在!");
return;
}