1、Get/Post
- public static void main(String[] args) {
- HttpClient client = new HttpClient();
- GetMethod getMethod = null;
- try {
- getMethod = new GetMethod("http://10.20.136.25/versionData.htm");
- int statusCode = client.executeMethod(getMethod);
- if (statusCode == 200) {
- String body = getMethod.getResponseBodyAsString();
- String[] cacheFiles = body.split("\\|");
- for (String cacheFile : cacheFiles) {
- String[] pairs = cacheFile.split("=");
- //one and only one pair
- String filename = pairs[0];
- String indicateNumber = pairs[1];
- System.out.println("file:" + filename + ", md5:"
- + indicateNumber.split("\\.")[0] + ", dependency md5:"
- + indicateNumber.split("\\.")[1]);
- }
- }
- } catch (HttpException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (getMethod != null) {
- getMethod.releaseConnection();
- }
- }
- }
2.https
如何支持https?
java 代码
- static{
- Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
- Protocol.registerProtocol("https", easyhttps);
- }
在执行具体的http method之前,将https协议注册一把
3.cookies
java 代码
- HttpClient httpclient=new HttpClient();
- httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);//RFC_2109是支持较普遍的一个,还有其他cookie协议
- HttpState initialState = new HttpState();
- Cookie cookie=new Cookie();
- cookie.setDomain("www.balblabla.com");
- cookie.setPath("/");
- cookie.setName("多情环");
- cookie.setValue("多情即无情");
- initialState.addCookie(cookie);
- httpclient.setState(initialState);
- ...
4.解构cookies
java 代码
- ...//执行了某些get/post方法后
- Cookie[] cookies = httpclient.getState().getCookies();
- System.out.println("Present cookies: ");
- for (int i = 0; i < cookies.length; i++) {//循环结构零部件
- System.out.println(" - " + cookies[i].toExternalForm());
- System.out.println(" - domain=" + cookies[i].getDomain());
- System.out.println(" - path=" + cookies[i].getPath());
- ...
- }
5.post参数
post方法在提交参数对时:
java 代码
- PostMethod postMethod = new PostMethod("http://www.saybot.com/postme");
- NameValuePair[] postData = new NameValuePair[2];
- postData[0] = new NameValuePair("武器", "枪");
- postData[1] = new NameValuePair("什么枪", "神枪");
- postMethod.addParameters(postData);
- ...//出枪吧
6.代理(proxy)
代理,非常重要,尤其在局域网横行的年头,没有代理,你在公司上不了QQ,没有代理,google不了网页快照,代理之威,可比七星碧玉刀,无刀,在局域网和开发当中,一切白搭:
java 代码
- HttpClient httpclient=new HttpClient();
- httpClient.getHostConfiguration().setProxy("192.168.0.1", 9527);
- httpClient.getParams().setAuthenticationPreemptive(true);//重要!!!告诉httpclient,使用抢先认证,否则你会收到“你没有资格”的恶果
- /*
- 这一步也至关重要,MyProxyCredentialsProvider实现了org.apache.commons.httpclient.auth.CredentialsProvider接口,
- 返回代理的credential(username/password)*/
- httpClient.getParams().setParameter(CredentialsProvider.PROVIDER, new MyProxyCredentialsProvider());
- httpClient.getState().setProxyCredentials(
- new AuthScope("192.168.0.1",
- AuthScope.ANY_PORT, //任意端口哦,可要小心
- AuthScope.ANY_REALM),//任意域哦,可要小心
- new UsernamePasswordCredentials("username",//proxy的用户名
- "password"));//proxy的密码
7.
看httpclient的官方文档: jakarta.apache.org/commons/httpclient/userguide.html
看Hilton网友写的小葵花宝典笔记: www.cnjm.net/tech/article1155.html
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/1004358,如需转载请自行联系原作者