java net url post_java java.net.URLConnection 实现http get,post

抽象类 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值