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