public class httpclientStudy {
public static void main(String[] args) throws Exception {
// javaHttpClient1();
httpClientCookies();
}
private static void httpClientCookies() throws Exception{
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpGet get = new HttpGet("
http://localhost:8080/sgcms/back/logout.do");
HttpResponse httpResponse = httpClient.execute(get, httpContext);
HttpEntity entity = httpResponse.getEntity();
String charSet = null;
if(entity!=null){
charSet = EntityUtils.getContentCharSet(entity);
System.out.println(charSet);
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
String text = null;
String line = br.readLine();
StringBuffer sb = new StringBuffer();
while(line != null){
sb.append(line+"\n");
line = br.readLine();
}
is.close();
}
HttpPost httpPost = new HttpPost("
http://localhost:8080/sgcms/back/ajaxJsonLogin.do'");
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("admin.loginName","hasau"));
list.add(new BasicNameValuePair("admin.passwordMd5","hasau"));
list.add(new BasicNameValuePair("admin.authCode",""));
httpPost.setEntity(new UrlEncodedFormEntity(list,charSet));
httpResponse = httpClient.execute(httpPost);
entity = httpResponse.getEntity();
if(entity != null){
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = br.readLine();
String text = null;
StringBuffer sb = new StringBuffer();
while(line != null){
sb.append(line+"\n");
line=br.readLine();
}
System.out.println(sb.toString());
}
httpClient.getConnectionManager().shutdown();
}