抽象类 URLConnection 是所有类的超类,它代表应用程序和 URL 之间的通信链接。此类的实例可用于读取和写入此 URL 引用的资源。通常,创建一个到 URL 的连接需要几个步骤:
openConnection()connect()
对影响到远程资源连接的参数进行操作。
与资源交互;查询头字段和内容。
---------------------------->
时间通过在 URL 上调用 openConnection 方法创建连接对象。
处理设置参数和一般请求属性。
使用 connect 方法建立到远程对象的实际连接。
远程对象变为可用。远程对象的头字段和内容变为可访问。
使用以下方法修改设置参数:
setAllowUserInteraction
setDoInput
setDoOutput
setIfModifiedSince
setUseCaches
使用以下方法修改一般请求属性:
setRequestProperty
使用 setDefaultAllowUserInteraction 和 setDefaultUseCaches 可设置 AllowUserInteraction 和 UseCaches 参数的默认值。
上面每个 set 方法都有一个用于获取参数值或一般请求属性值的对应 get 方法。适用的具体参数和一般请求属性取决于协议。
在建立到远程对象的连接后,以下方法用于访问头字段和内容:
getContent
getHeaderField
getInputStream
getOutputStream
某些头字段需要经常访问。以下方法:
getContentEncoding
getContentLength
getContentType
getDate
getExpiration
getLastModifed
每个 HttpURLConnection 实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络。请求后在 HttpURLConnection 的 InputStream 或 OutputStream 上调用 close() 方法可以释放与此实例关联的网络资源,但对共享的持久连接没有任何影响。如果在调用 disconnect() 时持久连接空闲,则可能关闭基础套接字。
POST
HttpURLConnection connection = (HttpURLConnection) new URL("http://www.cnblogs.com/mvc/Blog/GetBlogSideBlocks.aspx").openConnection();//connection.addRequestProperty("encoding", "UTF-8");//添加请求属性
connection.setDoInput(true);//允许输入 是否从httpUrlConnection读入,默认情况下是true;
connection.setDoOutput(true);//允许输出 post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false;
connection.setRequestMethod("POST");//POST请求 要在获取输入输出流之前设置 否则报错
connection.setConnectTimeout(6000);//设置超时时间
connection.setUseCaches(false);//Post 请求不能使用缓存
connection.setRequestProperty("cache-control", "private");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("content-length", "5766");//content-type →application/json; charset=utf-8//connection.setRequestProperty("content-type", "application/json; charset=utf-8");
connection.setRequestProperty("date", "Wed, 24 Oct 2018 12:21:33 GMT");
connection.setRequestProperty("x-frame-options", "SAMEORIGIN");
connection.setRequestProperty("x-ua-compatible", "IE=10");//输出
OutputStream os = connection.getOutputStream();//此处getOutputStream会隐含的进行connect
OutputStreamWriter osw = newOutputStreamWriter(os);
BufferedWriter bw= newBufferedWriter(osw);
bw.write("blogApp=ooo0&showFlag=ShowRecentComment,ShowTopViewPosts,ShowTopFeedbackPosts,ShowTopDiggPosts");
bw.flush();//刷新对象输出流,将任何字节都写入潜在的流中//输入
InputStream in = connection.getInputStream();//将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
InputStreamReader isr = new InputStreamReader(in, "UTF-8");
BufferedReader br= newBufferedReader(isr);
String line;
StringBuilder sb= newStringBuilder();while ((line = br.readLine()) != null) {
System.out.println(line);
sb.append(line);
}
bw.close();
osw.close();
os.close();
br.close();
isr.close();
in.close();
String str= sb.toString();
GET
URLConnection connection = new URL("http://www.cnblogs.com/mvc/Blog/GetBlogSideBlocks.aspx?blogApp=ooo0&showFlag=ShowRecentComment,ShowTopViewPosts,ShowTopDiggPosts").openConnection();
InputStream in=connection.getInputStream();
InputStreamReader isr= new InputStreamReader(in, "utf-8");
BufferedReader br= newBufferedReader(isr);
String line;
StringBuilder sb= newStringBuilder();while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
isr.close();
in.close();
String str= sb.toString();
参考文章:https://www.cnblogs.com/jeffen/p/6937788.html
参考文档:http://www.runoob.com/manual/jdk1.6/java/net/URLConnection.html