URL的基本组成: 协议,主机名,端口号,资源名 例如: http://www.it315.org:8080/index.html 如果这个URL写成http://www.it315.org/index.html 那么客户端程序则会使用协议的默认端口号80去连接网络服务器 相对URL: /a.html ./a.html ../../a.html a.html /a.html : /表示主机上某种协议的根目录下的a.html文件 ../a.html :表示当前资源所在目录的父目录下的a.html文件,也就是说这个a.html要比这个路径被写的地方高一级,或者说是当前资源网上跳一个文件夹 , ./表示当前资源的文件夹,然后再来个.就表示这个文件和这个文件夹是平级的 ./a.html和a.html是一样的:表示a.html与当前资源平级 注意哈: 用平级来理解相对URL比较清晰 URL编码规则: 1。 将空格转换为加号(+) , 或者用%20 2.对于0-9,a-z,A-Z 之间的字符保持不变 3。对于所有其他的字符,用这个字符的当前字符集编码在内存中的十六进制格式表示,并且在每个子借钱加上一个%,如字符"+"用%2B表示,字符"="用%*D表示,字符“&”用%26表示,每个中文字符在内存中占两个字节,字符中的"中"用%D6%D0表示,字符"国"用%B9%FA表示。 Http协议定义了web服务器和web浏览器之间交换数据的会话过程,以及数据本身的格式 http1.1与http 1.0最大的区别就是支持持久性的连接 http1.0的交互过程 客户机 服务器 ------|----建立连接------->|-------- |-----发出请求信息--->| |<----发出响应信息----| ------|<----关闭连接--------|--------- 每次连接只处理一个请求,而且通信过程总是由web浏览器发送来启动,注意如果返回的网页中有src="XX",那么在浏览器收到这个东西后会再次发送请求来得到这个图片,所以又是一次建立连接和关闭连接的过程 http1.1的客户机与服务器的信息交互过程 ----建立连接----> ----发送第一次请求---> ----发送第二次请求(突然发现网页文档中有一个图片的地址,那么发送第二次请求)---> 。。。。。 <---发送第一次响应---- <---发送第二次响应---- 。。。。 ----发送关闭连接请求----> <---关闭连接-------- 这些发出的请求 和发出的响应都称为http消息 请求的http: 请求行,若干个消息头,以及实体内容,消息头和实体内容是可选的 响应的 http: 状态行,若干个消息头,以及实体内容,消息头和实体内容是可选的 下面用telnet来发送http telnet www.it315.org 80 //这里是连接这个服务器 GET / HTTP/1.1 //如果/下面什么都不写,那么是得到首页 HOST: //这个字段是必不可少的,但是可以为空,对于使用虚拟主机的服务器来说,你还需要在这里填入域名。 //然后按下回车,我们就能在dos下看见返回的网页源文件了 然后我们继续访问 GET /images.gif HTTP/1.1 HOST: Connection:close //这个值是我们希望服务器返回值后关闭连接 //回车两下就可以了 Http的几个消息头: Connection: 用于制定处理完本次请求响应后,客户端与服务器是否继续保持连接,设置值我可以为Keep-Alive和close 注意:http/1.1默认使用的是Keep-Alive ,保持连接(或者叫持久性连接),也就是说如果我们不指定Connection,那么他就会是Keep-Alive Accept-Language:zh-cn //那么有的服务器会解析这个头部,返回相应文字的http Range 用于指定服务器只需返回文档中的部分内容以及内容范围,这对较大文档的断点续传非常有用 Range bytes= 100-599 //要求服务器返回从第100到第599个字节的内容 Range bytes=100- //要求返回从第100到最后 Range bytes= -100 //返回最后100个字节的内容 响应消息头: Content-Range 用于指定服务器返回的部分实体内容的位置信息,例如 Content-Range bytes 2543-4532/7898 //我返回了从2543到4532的内容,全篇总长为7898 URL 类 public URL(String spec) ;//直接使用标示url的字符串来创建对象 public URL(String protocol,String host,int port,String file);//file为资源名 public URL(String protocol,String host,int port,String file,URLStreamHandler handler);//在第二个构造函数基础上指定一个对这个URL处理的协议处理器 public URL(URL context,String spec);//也就是这两者的组合变成一个新的URL getProtocol,getHost,getPort,getFile URL本身不能获取网页文档中的具体的内容,协议处理器可以啊 openConntection()//这个方法会调用协议处理器的openConnection方法,返回一个URLConnection对象,用这个对象就可以获取具体内容,因为他有协议处理器嘛 URL类的setURLStreamHandlerFactory(URLStreamHandlerFactory fac);静态方法 URLStreamHandlerFactory fac//这个东西用于建立协议名和协议之间的对应关系,比如指定的协议名为http,那么就应该调用http协议处理器 注意: JVM会根据出现的协议名调用 StreamHandlerFactory对象中的createURLStreamHandler(String protocol)方法,将协议名传递给方法,这个方法会创建相应的协议处理器对象给URL对象 URLConnection与HttpURLConnection类 URLConnection并没有连接,只有调用connect方法时才连接 在调用connect方法前可以设置头信息:setRequestProperty方法 在调用connect方法后可以调用各种get方法,比如getHeaderFields方法 getInputStream和getOutputStream方法 getHeaderField,getContentLength,getContentEncoding.... 注意:一个HttpURLConnection对象一次用于一次会话请求 也就是说他只能向服务器发送一次请求,然后得到服务器的一次响应结果,他不能向服务器发送第二次请求,(也就是说他只能调用一次connect()方法)但是客户端程序和服务器建立的一个Http连接可以被多个HttpURLConnction实例对象共享,这样就可以利用http/1.1的持续连接特性,调用HttpURLConnection的disconnect方法可以关闭底层共享网络。 HttpURLConnction的close方法不会影响到底层的网络链接,只会关闭他自己的资源 所以要注意:disconnect方法和close方法的不同哈,注意,这里将的close方法在HttpURLConnction上没有,是在他的流对象上的哦。。。。 System.out.println("\n"); //这是换两行 URL urlGoogle = new URL("http://www.google.com"); HttpURLConnection conn = (HttpURLConnection)URL.openConnection(); conn.setProperty("Accept-Language","ja"); Map m = conn.getRequestProperties(); //虽然这里是get,但是这是get请求消息头,所以说明他还没有连接上,只有得到响应消息或者直接调用conncet方法才能连接上 Set keys = m.keySet(); for(Iterator it = keys.iterator();it.hasNext();){ String field = (String)it.next(); System.out.println(field+" : "+conn.getRequestProperty(field)); } conn.connect(); //这个也可以不写,如果现实调用响应信息的话 Map m = conn.getResponseProperties(); //虽然这里是get,但是这是get请求消息头,所以说明他还没有连接上,只有得到响应消息或者直接调用conncet方法才能连接上 Set keys = m.keySet(); for(Iterator it = keys.iterator();it.hasNext();){ String field = (String)it.next(); System.out.println(field+" : "+conn.getResponseProperty(field)); } ---------------------用下面这个比较好------------------------ public class HttpClientUtils { private static final Log logger = LogFactory.getLog(HttpClientUtils.class); private static HttpClient client; private static int HTTPCLIENT_CONNECT_TIMEOUT = 10000; private static int HTTPCLIENT_RESPONSE_TIMEOUT = 10000; static { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); client = new HttpClient(connectionManager); } public static String getResult(String url) throws Exception { HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams(); // 设置连接超时时间(单位毫秒) managerParams.setConnectionTimeout(HTTPCLIENT_CONNECT_TIMEOUT); // 设置读数据超时时间(单位毫秒) managerParams.setSoTimeout(HTTPCLIENT_RESPONSE_TIMEOUT); StringBuffer requestUrl = new StringBuffer(); requestUrl.append(url); HttpMethod method = new GetMethod(requestUrl.toString()); byte[] resultBytes; try { int statusCode = client.executeMethod(method); if (statusCode == 404) throw (new Exception("404 Error")); resultBytes = method.getResponseBody(); return new String(resultBytes); } catch (Exception e) { logger.error("404 Error", e); throw new Exception(e); } finally { method.releaseConnection(); } } public static String postInfo(String url, String value) throws Exception { HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams(); // 设置连接超时时间(单位毫秒) managerParams.setConnectionTimeout(HTTPCLIENT_CONNECT_TIMEOUT); // 设置读数据超时时间(单位毫秒) managerParams.setSoTimeout(HTTPCLIENT_RESPONSE_TIMEOUT); PostMethod method = new PostMethod(url); method.setRequestEntity(new StringRequestEntity(value, "text/xml", "UTF-8")); byte[] resultBytes; try { int statusCode = client.executeMethod(method); if (statusCode == 404) throw (new Exception("404 Error")); resultBytes = method.getResponseBody(); return new String(resultBytes); } catch (Exception e) { logger.error("404 Error", e); throw new Exception(e); } finally { method.releaseConnection(); } } } ----------------------------------------httpClient大全 1.package pack; 2.import org.apache.commons.httpclient.*; 3.import org.apache.commons.httpclient.cookie.*; 4.import org.apache.commons.httpclient.methods.*; 5.import java.io.*; 6.7.public class MyHttpClient { 8. private HttpClient client; 9. 10. //11. // if you kown the host 12. // ok13. public MyHttpClient(String host) { 14. client = new HttpClient(); 15. client.getHostConfiguration().setHost(host, 80, "http"); 16. } 17. 18. //19. // or you don't kown 20. //21. public MyHttpClient() { 22. client = new HttpClient(); 23. client.getHostConfiguration().setHost("", 80, "http"); 24. } 25. 26. 27. // login user->username password->userPassword url28. // Usually a successful form-based login results in a redicrect to 29. // another url 30. 31. //usually user and password 32. //sometimes id and password33. //so we can use in this mode34. public String login(String user, String userName, String password, 35. String userPassword, String url) throws Exception 36. { 37. client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); 38. 39. PostMethod post = new PostMethod(url); 40. NameValuePair nuser = new NameValuePair(user, userName); 41. NameValuePair npass = new NameValuePair(password, userPassword); 42. 43. return postToServer(new NameValuePair[]{nuser, npass}, post); 44.45. } 46. 47. //default email and password48. public String login(String email, String password, String url) 49. throws Exception 50. { 51. 52. client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); 53. 54. PostMethod post = new PostMethod(url); 55. NameValuePair nuser = new NameValuePair("email", email); 56. NameValuePair npass = new NameValuePair("password", password); 57. return postToServer(new NameValuePair[] {nuser, npass}, post); 58. } 59. 60. //post login msg to server61. public String postToServer(NameValuePair[] nameValuePairs, PostMethod post) 62. throws Exception 63. { 64. String redicretURL = null; 65. post.setRequestBody(nameValuePairs); 66. 67. client.executeMethod(post); 68. post.releaseConnection(); 69. int statusCode = post.getStatusCode(); 70. if ((statusCode == HttpStatus.SC_MOVED_TEMPORARILY) 71. || (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) 72. || (statusCode == HttpStatus.SC_SEE_OTHER) 73. || (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) { 74. Header header = post.getResponseHeader("location"); 75. if (header != null) { 76. redicretURL = new String(header.getValue()); 77. } 78. } 79. return redicretURL; 80. } 81. 82. //83. //get html stream84. //85. public InputStream getContentAsStream(String url) throws Exception { 86. GetMethod get = new GetMethod(url); 87. client.executeMethod(get); 88. return get.getResponseBodyAsStream(); 89. } 90. 91. //92. //get html string93. //94. public String getContentAsString(String url) throws Exception { 95. GetMethod get = new GetMethod(url); 96. client.executeMethod(get); 97. return get.getResponseBodyAsString(); 98. } 99. 100.} 101.102.下面给出一个例子: 比如说登录一个网站: 1.package pack; 2.3.public class Login { 4.5. public static void main(String[] args) throws Exception { 6. // TODO Auto-generated method stub7. //now, i try to sign in xiaonei8. 9. MyHttpClient httpClient = new MyHttpClient(); 10. String redicretURL = new String(); 11. StringBuffer index = new StringBuffer(); 12. 13. 14. redicretURL = httpClient.login("email", "password", "site"); 15. if (redicretURL != null) { 16. index.append(httpClient.getContentAsString(redicretURL)); 17. System.out.println(index); 18. } else { 19. System.out.println(redicretURL); 20. } 21. 22. } 23.24.} 25.26.