ImageVerifierCode 换一换
格式:DOCX , 页数:22 ,大小:22.79KB ,
资源ID:8019783      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8019783.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(httpClient教程.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

httpClient教程.docx

1、httpClient教程httpclient教程 Commons-httpclient项目就是专门设计来简化HTTP客户端与服务器进行各种通讯编程。 1 读取网页(HTTP/HTTPS)内容 最简单的HTTP客户端,用来演示通过GET或者POST方式访问某个页面 Java代码 1. packagehttp.demo; 2. 3. importjava.io.IOException; 4. importmons.httpclient.*; 5. importmons.httpclient.methods.*; 6. 7. publicclassSimpleClient 8. 9. publics

2、taticvoidmain(Stringargs)throwsIOException 10. 11. HttpClientclient=newHttpClient(); 12. 13. /设置代理服务器地址和端口 14. 15. /client.getHostConfiguration().setProxy(proxy_host_addr,proxy_port); 16. 17. /使用GET方法,如果服务器需要通过HTTPS连接,那只需要将下面URL中的http换成https 18. 19. HttpMethodmethod=newGetMethod(); 20. 21. /使用POST方法

3、 22. 23. /HttpMethodmethod=newPostMethod(); 24. 25. client.executeMethod(method); 26. 27. /打印服务器返回的状态 28. 29. System.out.println(method.getStatusLine(); 30. 31. /打印返回的信息 32. 33. System.out.println(method.getResponseBodyAsString(); 34. 35. /释放连接 36. 37. method.releaseConnection(); 38. 39. 40. package

4、 http.demo;import java.io.IOException;import mons.httpclient.*;import mons.httpclient.methods.*;public class SimpleClient public static void main(String args) throws IOException HttpClient client = new HttpClient(); /设置代理服务器地址和端口 /client.getHostConfiguration().setProxy(proxy_host_addr,proxy_port); /

5、使用GET方法,如果服务器需要通过HTTPS连接,那只需要将下面URL中的http换成https HttpMethod method = new GetMethod(); /使用POST方法 /HttpMethod method = new PostMethod(); client.executeMethod(method); /打印服务器返回的状态 System.out.println(method.getStatusLine(); /打印返回的信息 System.out.println(method.getResponseBodyAsString(); /释放连接 method.relea

6、seConnection(); 2 以GET或者POST方式向网页提交参数 Java代码 1. packagehttp.demo; 2. 3. importjava.io.IOException; 4. 5. importmons.httpclient.*; 6. 7. importmons.httpclient.methods.*; 8. 9. /* 10. 11. *提交参数演示 12. 13. *该程序连接到一个用于查询手机号码所属地的页面 14. 15. *以便查询号码段1330227所在的省份以及城市 16. 17. */18. 19. publicclassSimpleHttpCl

7、ient 20. 21. publicstaticvoidmain(Stringargs)throwsIOException 22. 23. 24. 25. HttpClientclient=newHttpClient(); 26. 27. client.getHostConfiguration().setHost(,80,http); 28. 29. HttpMethodmethod=getPostMethod();/使用POST方式提交数据 30. 31. client.executeMethod(method); 32. 33. /打印服务器返回的状态 34. 35. System.ou

8、t.println(method.getStatusLine(); 36. 37. /打印结果页面 38. 39. Stringresponse= 40. 41. newString(method.getResponseBodyAsString().getBytes(8859_1); 42. 43. /打印返回的信息 44. 45. System.out.println(response); 46. 47. method.releaseConnection(); 48. 49. 50. 51. /* 52. 53. *使用GET方式提交数据 54. 55. *return 56. 57. */

9、58. 59. privatestaticHttpMethodgetGetMethod() 60. 61. returnnewGetMethod(/simcard.php?simcard=1330227); 62. 63. 64. 65. /* 66. 67. *使用POST方式提交数据 68. 69. *return 70. 71. */72. 73. privatestaticHttpMethodgetPostMethod() 74. 75. PostMethodpost=newPostMethod(/simcard.php); 76. 77. NameValuePairsimcard=n

10、ewNameValuePair(simcard,1330227); 78. 79. post.setRequestBody(newNameValuePairsimcard); 80. 81. returnpost; 82. 83. 84. 85. package http.demo;import java.io.IOException;import mons.httpclient.*;import mons.httpclient.methods.*;/* 提交参数演示* 该程序连接到一个用于查询手机号码所属地的页面* 以便查询号码段1330227所在的省份以及城市*/public class

11、SimpleHttpClient public static void main(String args) throws IOException HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(, 80, http); HttpMethod method = getPostMethod();/使用POST方式提交数据 client.executeMethod(method); /打印服务器返回的状态 System.out.println(method.getStatusLine(); /打印

12、结果页面 String response = new String(method.getResponseBodyAsString().getBytes(8859_1); /打印返回的信息 System.out.println(response); method.releaseConnection(); /* * 使用GET方式提交数据 * return */ private static HttpMethod getGetMethod() return new GetMethod(/simcard.php?simcard=1330227); /* * 使用POST方式提交数据 * return

13、 */ private static HttpMethod getPostMethod() PostMethod post = new PostMethod(/simcard.php); NameValuePair simcard = new NameValuePair(simcard,1330227); post.setRequestBody(new NameValuePair simcard); return post; 3 处理页面重定向 详细描述: 状态码 对应HttpServletResponse的常量 301 SC_MOVED_PERMANENTLY 页面已经永久移到另外一个新地址

14、 302 SC_MOVED_TEMPORARILY 页面暂时移动到另外一个新的地址 303 SC_SEE_OTHER 客户端请求的地址必须通过另外的URL来访问 307 SC_TEMPORARY_REDIRECT 同 SC_MOVED_TEMPORARILY 下面的代码片段演示如何处理页面的重定向 Java代码 1. client.executeMethod(post); 2. 3. System.out.println(post.getStatusLine().toString(); 4. 5. post.releaseConnection(); 6. 7. /检查是否重定向 8. 9. i

15、ntstatuscode=post.getStatusCode(); 10. 11. if(statuscode=HttpStatus.SC_MOVED_TEMPORARILY)| 12. 13. (statuscode=HttpStatus.SC_MOVED_PERMANENTLY)| 14. 15. (statuscode=HttpStatus.SC_SEE_OTHER)| 16. 17. statuscode=HttpStatus.SC_TEMPORARY_REDIRECT) 18. 19. 20. /读取新的URL地址 21. 22. Headerheader=post.getResp

16、onseHeader(location); 23. 24. if(header!=null) 25. 26. 27. Stringnewuri=header.getValue(); 28. 29. if(newuri=null)|(newuri.equals() 30. 31. newuri=/; 32. 33. 34. GetMethodredirect=newGetMethod(newuri); 35. 36. client.executeMethod(redirect); 37. 38. System.out.println(Redirect:+ 39. redirect.getStat

17、usLine().toString(); 40. 41. redirect.releaseConnection(); 42. 43. else44. 45. System.out.println(Invalidredirect); 46. 47. client.executeMethod(post); System.out.println(post.getStatusLine().toString(); post.releaseConnection(); /检查是否重定向 int statuscode = post.getStatusCode(); if (statuscode = HttpS

18、tatus.SC_MOVED_TEMPORARILY) | (statuscode = HttpStatus.SC_MOVED_PERMANENTLY) | (statuscode = HttpStatus.SC_SEE_OTHER) | statuscode = HttpStatus.SC_TEMPORARY_REDIRECT) /读取新的URL地址 Header header = post.getResponseHeader(location); if (header != null) String newuri = header.getValue(); if (newuri = null

19、) | (newuri.equals() newuri = /; GetMethod redirect = new GetMethod(newuri); client.executeMethod(redirect); System.out.println(Redirect:+ redirect.getStatusLine().toString(); redirect.releaseConnection(); else System.out.println(Invalid redirect); 4 模拟输入用户名和口令进行登录 本小节应该说是HTTP客户端编程中最常碰见的问题,很多网站的内容都只

20、是对注册用户可见的,这种情况下就必须要求使用正确的用户名和口令登录成功后,方可浏览到想要的页面。因为HTTP协议是无状态的,也就是连接的有效期只限于当前请求,请求内容结束后连接就关闭了。在这种情况下为了保存用户的登录信息必须使用到Cookie机制。以JSP/Servlet为例,当浏览器请求一个JSP或者是Servlet的页面时,应用服务器会返回一个参数,名为jsessionid(因不同应用服务器而异),值是一个较长的唯一字符串的Cookie,这个字符串值也就是当前访问该站点的会话标识。浏览器在每访问该站点的其他页面时候都要带上jsessionid这样的Cookie信息,应用服务器根据读取这个会

21、话标识来获取对应的会话信息。 对于需要用户登录的网站,一般在用户登录成功后会将用户资料保存在服务器的会话中,这样当访问到其他的页面时候,应用服务器根据浏览器送上的Cookie中读取当前请求对应的会话标识以获得对应的会话信息,然后就可以判断用户资料是否存在于会话信息中,如果存在则允许访问页面,否则跳转到登录页面中要求用户输入帐号和口令进行登录。这就是一般使用JSP开发网站在处理用户登录的比较通用的方法。 对于HTTP的客户端来讲,如果要访问一个受保护的页面时就必须模拟浏览器所做的工作,首先就是请求登录页面,然后读取Cookie值;再次请求登录页面并加入登录页所需的每个参数;最后就是请求最终所需的

22、页面。当然在除第一次请求外其他的请求都需要附带上 Cookie信息以便服务器能判断当前请求是否已经通过验证。 Java代码 1. packagehttp.demo; 2. 3. importmons.httpclient.*; 4. 5. importmons.httpclient.cookie.*; 6. 7. importmons.httpclient.methods.*; 8. 9. /* 10. 11. *用来演示登录表单的示例 12. 13. */14. 15. publicclassFormLoginDemo 16. 17. staticfinalStringLOGON_SITE=

23、localhost; 18. 19. staticfinalintLOGON_PORT=8080; 20. 21. 22. 23. publicstaticvoidmain(Stringargs)throwsException 24. 25. HttpClientclient=newHttpClient(); 26. 27. client.getHostConfiguration().setHost(LOGON_SITE,LOGON_PORT); 28. 29. 30. 31. /模拟登录页面login.jsp-main.jsp 32. 33. PostMethodpost=newPostMe

24、thod(/main.jsp); 34. 35. NameValuePairname=newNameValuePair(name,ld); 36. 37. NameValuePairpass=newNameValuePair(password,ld); 38. 39. post.setRequestBody(newNameValuePairname,pass); 40. 41. intstatus=client.executeMethod(post); 42. 43. System.out.println(post.getResponseBodyAsString(); 44. 45. post.releaseConnection(); 46. 47. 48. 49. /查看cookie信息 50. 51. CookieSpeccookiespec=CookiePolicy.getDefaultSpec(); 52. 53. Cookiecookies=cookiespec.match(LOGON_SITE,LOGON_PORT,/,false,client.getState().getCookies(); 54. 55. if(cookies.length=0) 56. 57. System.out.println(None); 58

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1