做了好几个httpclient访问网站的项目了,我发布一个可以直接使用的类供大家参考吧;希望对想用httpclient的人有所帮助。
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* @author Stony Zhang (Mybeautiful)
* @MSN stonyz@live.com
* @QQ 55279427
*
*/
public class Netway {
private HttpClient httpClient = new HttpClient();
private String serverURL = "";
public String postPage(String page) {
String url = this.getURL(page);
PostMethod postMethod = new PostMethod(url);
return this.post(postMethod);
}
private String getURL(String page) {
// TODO Auto-generated method stub
return "http://" + this.serverURL + "/" + page;
}
public String postPage(String page, Properties pdata) {
String url = this.getURL(page);
PostMethod postMethod = new PostMethod(url);
NameValuePair[] data = new NameValuePair[pdata.size()];
Enumeration keys = pdata.keys();
int i = 0;
for (; keys.hasMoreElements();) {
String k = (String) keys.nextElement();
String v = (String) pdata.get(k);
data[i] = new NameValuePair(k, v);
i++;
}
postMethod.setRequestBody(data);
return this.post(postMethod);
}
private String post(PostMethod postMethod) {
String result = "";
try {
int statusCode = httpClient.executeMethod(postMethod);
// 301 or 302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
Header locationHeader = postMethod
.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out
.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
}
result = postMethod.getResponseBodyAsString();
} catch (HttpException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(result);
return result;
}
public void setServerURL(String serverURL) {
this.serverURL = serverURL;
}
public String getServerURL() {
return serverURL;
}
}
分享到:
相关推荐
HttpClient是个很不错的开源框架(org.appache.http),封装了访问http的请求头,参数,内容体,响应等等,使用起来更方面更强大。 HttpURLConnection是java的标准类,可以实现简单的基于URL请求、响应功能,什么都...
http访问工具类。 自动维护session
用于模拟浏览器访问网页,抓取站点源文件。 HttpClient(现在是HttpComponents)把问题搞复杂了。其实Java内置的HttpURLConnection很好的。HttpComponents不但复杂,而且有小问题。Java API没有内置...
android Http异步网络访问框架,对耗时的大数据量的网络数据获取,封装工具类
Apache HttpClient是一个完善的HTTP客户端,它提供了对HTTP协议的全面支持,可以使用HTTP GET和POST进行访问。下面我们就结合实例,介绍一下HttpClient的使用方法。 我们新建一个http项目,项目结构如图: 在这个...
网络请求在现代的应用开发中必不可少,我们熟知的网络请求框架还真不少,像HttpClient、OKHttp还有volley等,它们确实方便,但鸿蒙还不能使用它们,还好我们有HttpURLConnection。原始的网络访问,再加上多线程,会...
为了更好的在应对网络访问,学习下okhttp还是蛮必要的,本篇博客首先介绍okhttp的简单使用,主要包含: 一般的get请求 一般的post请求 基于Http的文件上传 文件下载 加载图片 支持请求回调,直接返回对象、...
RestTemplete远程调用技术,升级版的httpclient,对httpclient进行了get post等方法进行了封装,提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。我的代码中包含了get post 的多种访问方式,...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
随机访问文件: RandomAccessFile 02 Http 协议的 请求头 的 Range 分段请求资源 03 Http网络请求: HttpURLConnection 使用(基于 HttpURLConnection 封装的 HttpClient) 04 线程池的使用: ThreadPoolExecutor / ...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...