HttpClient4
来源:互联网 发布:小说南风知我意2 编辑:程序博客网 时间:2024/05/16 12:38
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;
}
}
0 0
- HTTPClient4
- HttpClient4
- HttpClient4使用
- HttpClient4.0
- Httpclient4 简介
- HttpClient4.3
- Httpclient4.5
- httpclient4.5
- HttpClient4.5
- httpClient4.5
- HttpClient4.3.1
- httpclient4的封装
- HttpClient4文件上传
- httpclient4的封装
- httpclient4登录网站
- httpclient4 超时时间
- HttpClient4 Post XML数据
- HttpClient4学习笔记
- 在多层Activity里面直接退出app(二)
- 基于libuv库的tcp, udp echo服务器和客户段测试python代码。
- 神秘顾客调查的作用
- Warning: World-writable config file '/etc/mysql/my.cnf' is ignored
- Spring 注解学习手札(三) 表单页面处理
- HttpClient4
- Performance Tips for Android’s ListView
- iOS随笔之获取可用空间及某个文件或文件夹的大小
- 一些感受
- Android 有时接收不到自己发送的广播消息
- myeclipse安装svn插件的多种方式
- Hadoop学习笔记_安装篇之二:HIVE安装
- monkey test学习
- 8个超实用的jQuery技巧攻略