public class HttpClientUtil {
private static DefaultHttpClient client = new DefaultHttpClient();
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/*
* POST_CHARSET 请求的FORM页面编码
*/
public static String sendPost(String url, List<NameValuePair> postParams,String POST_CHARSET)
throws Exception {
HttpPost post = new HttpPost(url);
post.setHeader("User-Agent", USER_AGENT);
post.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
post.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
post.setHeader("Connection", "keep-alive");
// 设置请求的编码格式
post.setEntity(new UrlEncodedFormEntity(postParams, POST_CHARSET));
HttpResponse response = client.execute(post);
int responseCode = response.getStatusLine().getStatusCode();
BufferedReader rd = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
post.abort();
return String.valueOf(responseCode);
}
/*
* PAGECHARSET 页面编码
*/
public static String GetPageContent(String url,String PAGECHARSET) throws Exception {
HttpGet get = new HttpGet(url);
get.setHeader("User-Agent", USER_AGENT);
get.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
get.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
get.setHeader("Connection", "keep-alive");
HttpResponse response = client.execute(get);
int responseCode = response.getStatusLine().getStatusCode();
BufferedReader rd = new BufferedReader(new InputStreamReader(response
.getEntity().getContent(),PAGECHARSET));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
get.abort();
return result.toString();
}
public static void GetPageContentToFile(String url, String target)
throws Exception {
HttpGet get = new HttpGet(url);
get.setHeader("User-Agent", USER_AGENT);
get.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
get.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
get.setHeader("Connection", "keep-alive");
HttpResponse response = client.execute(get);
int responseCode = response.getStatusLine().getStatusCode();
InputStream in = response.getEntity().getContent();
FileOutputStream fos = new FileOutputStream(target);
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
fos.write(buffer);
}
in.close();
fos.close();
System.out.println("Source:" + url + " Target:" + target);
}
public static List<NameValuePair> getFormParams(String url, String username,
String password) throws UnsupportedEncodingException {
Document doc = Jsoup.parse(url);
// Google form id
Element loginform = doc.getElementById("gaia_loginform");
// getByName Element form = doc.getElementsByAttributeValue("name", "XX").first();
Elements inputElements = loginform.getElementsByTag("input");
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Element inputElement : inputElements) {
String key = inputElement.attr("name");
String value = inputElement.attr("value");
if (key.equals("Email"))
value = username;
else if (key.equals("Passwd"))
value = password;
paramList.add(new BasicNameValuePair(key, value));
}
return paramList;
}
}
private static DefaultHttpClient client = new DefaultHttpClient();
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/*
* POST_CHARSET 请求的FORM页面编码
*/
public static String sendPost(String url, List<NameValuePair> postParams,String POST_CHARSET)
throws Exception {
HttpPost post = new HttpPost(url);
post.setHeader("User-Agent", USER_AGENT);
post.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
post.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
post.setHeader("Connection", "keep-alive");
// 设置请求的编码格式
post.setEntity(new UrlEncodedFormEntity(postParams, POST_CHARSET));
HttpResponse response = client.execute(post);
int responseCode = response.getStatusLine().getStatusCode();
BufferedReader rd = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
post.abort();
return String.valueOf(responseCode);
}
/*
* PAGECHARSET 页面编码
*/
public static String GetPageContent(String url,String PAGECHARSET) throws Exception {
HttpGet get = new HttpGet(url);
get.setHeader("User-Agent", USER_AGENT);
get.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
get.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
get.setHeader("Connection", "keep-alive");
HttpResponse response = client.execute(get);
int responseCode = response.getStatusLine().getStatusCode();
BufferedReader rd = new BufferedReader(new InputStreamReader(response
.getEntity().getContent(),PAGECHARSET));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
get.abort();
return result.toString();
}
public static void GetPageContentToFile(String url, String target)
throws Exception {
HttpGet get = new HttpGet(url);
get.setHeader("User-Agent", USER_AGENT);
get.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
get.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
get.setHeader("Connection", "keep-alive");
HttpResponse response = client.execute(get);
int responseCode = response.getStatusLine().getStatusCode();
InputStream in = response.getEntity().getContent();
FileOutputStream fos = new FileOutputStream(target);
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
fos.write(buffer);
}
in.close();
fos.close();
System.out.println("Source:" + url + " Target:" + target);
}
public static List<NameValuePair> getFormParams(String url, String username,
String password) throws UnsupportedEncodingException {
Document doc = Jsoup.parse(url);
// Google form id
Element loginform = doc.getElementById("gaia_loginform");
// getByName Element form = doc.getElementsByAttributeValue("name", "XX").first();
Elements inputElements = loginform.getElementsByTag("input");
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Element inputElement : inputElements) {
String key = inputElement.attr("name");
String value = inputElement.attr("value");
if (key.equals("Email"))
value = username;
else if (key.equals("Passwd"))
value = password;
paramList.add(new BasicNameValuePair(key, value));
}
return paramList;
}
}