转载(http://www.cnblogs.com/shyang--TechBlogs/archive/2011/03/21/1990525.html)
关于URLConnection,网上很多回答都是对API的翻译,很崩溃,我是看了很多之后,然后看API才发现的。此后我会吸取教训,一遇到问题首先看API,也建议大家这样,英文的最好,不是太相信那些翻译的,呵呵,感觉还是自己斟酌理解的好吧。下面我就解读一下URLConnection的API,虽然也是一种翻译,但是结合我自己的理解,还有一点亲自调试得出的结论。
URLConnection是所有表示应用程序与URL之间通信连接的类的父类(super class),该类的实例可以用来对由URL引用的资源进行读取和写入操作。
HttpURLConnection 是支持HTTP特定功能的URLConnection,还有JarConnection是URLConnection的直接子类。
创建URLConnection对象主要要经历两步:
第一步是creat:URLConnection conn = url.openConnection();
第二步是connect:conn.connect();//这两个是不同的,见下面调试
在created和connected之间可以设置一些变量选项(如setDoInput,超时等),而如果connect之后再设置就会引发异常(网上也有这个问题)
在需要连接才能执行的操作(如getInputStream等应用层操作),程序会暗中(implicitly)执行连接
一旦连接可用,就可以访问获取资源,如执行getInputStream()等,对于HttpURLConnection,还有conn.getResponseCode()==200来判定服务器是否返回正确的应答码以表明请求被接受。
在URLConnection中,有一个域boolean connected ,值为true表明已经建立到指定URL的连接;false则没有(华丽的标记)
connect() 当连接还未被建立时,打开一个communications link,而如果这个链接已经被打开(connected值设置为true)则 ignore it 。
下面编了一个小程序进行调试:
URL url = new URL("http://www.google.cn");
URLConnection conn = url.openConnection();
conn.setConnectTimeout(10000);
conn.connect();
InputStream inStream = conn.getInputStream();
当程序执行完openConnection()之后,域connected值还是false,说明这时候还未连接。等执行到connect()之后,connected才变为true,说明这时候才完成连接。而当我注释掉connect()后,再运行程序,connected值到getInputStream执行完又变为true,这时候知道getInputStream会使连接暗中被执行。
综上,对于一般需要连接才执行的操作(可能也有不需要连接的操作吧~~),connect()是可以省略的。但是只有在connect()执行完,连接才正式建立,而不是openConnection(只是创建了一个连接对象)
示列
转载(http://caspers.iteye.com/blog/212876#)
1 package com.test; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 import java.io.OutputStreamWriter; 8 import java.net.URL; 9 import java.net.URLConnection; 10 11 public class TestPost { 12 13 public static void testPost() throws IOException { 14 15 /** 16 * 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using 17 * java.net.URL and //java.net.URLConnection 18 */ 19 URL url = new URL("http://www.faircanton.com/message/check.asp"); 20 URLConnection connection = url.openConnection(); 21 /** 22 * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。 23 * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做: 24 */ 25 connection.setDoOutput(true); 26 /** 27 * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ... 28 */ 29 OutputStreamWriter out = new OutputStreamWriter(connection 30 .getOutputStream(), "8859_1"); 31 out.write("username=kevin&password=*********"); //post的关键所在! 32 // remember to clean up 33 out.flush(); 34 out.close(); 35 /** 36 * 这样就可以发送一个看起来象这样的POST: 37 * POST /jobsearch/jobsearch.cgi HTTP 1.0 ACCEPT: 38 * text/plain Content-type: application/x-www-form-urlencoded 39 * Content-length: 99 username=bob password=someword 40 */ 41 // 一旦发送成功,用以下方法就可以得到服务器的回应: 42 String sCurrentLine; 43 String sTotalString; 44 sCurrentLine = ""; 45 sTotalString = ""; 46 InputStream l_urlStream; 47 l_urlStream = connection.getInputStream(); 48 // 传说中的三层包装阿! 49 BufferedReader l_reader = new BufferedReader(new InputStreamReader( 50 l_urlStream)); 51 while ((sCurrentLine = l_reader.readLine()) != null) { 52 sTotalString += sCurrentLine + "\r\n"; 53 54 } 55 System.out.println(sTotalString); 56 } 57 58 public static void main(String[] args) throws IOException { 59 60 testPost(); 61 62 } 63 64 }