码迷,mamicode.com
首页 > 其他好文 > 详细

URLConnection 详细

时间:2014-07-05 18:11:08      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   width   

通信链接。程序可以通过URLConnection实例向该URL发送请求、读取URL引用的资源。

通常创建一个和 URL的连接,并发送请求、读取此URL引用的资源需要如下几个步骤:

通过调用URL对象openConnection()方法来创建URLConnection对象。

设置URLConnection的参数和普通请求属性。

如果只是发送GET方式请求,使用connect方法建立和远程资源之间的实际连接即可;如果需要发送POST方式的请求,需要获取URLConnection实例对应的输出流来发送请求参数。

远程资源变为可用,程序可以访问远程资源的头字段或通过输入流读取远程资源的数据。

在建立和远程资源的实际连接之前,程序可以通过如下方法来设置请求头字段:

setAllowUserInteraction:设置该URLConnection的allowUserInteraction请求头字段的值。
setDoInput:设置该URLConnection的doInput请求头字段的值。
setDoOutput:设置该URLConnection的doOutput请求头字段的值。
setIfModifiedSince:设置该URLConnection的ifModifiedSince请求头字段的值。
setUseCaches:设置该URLConnection的useCaches请求头字段的值。

除此之外,还可以使用如下方法来设置或增加通用头字段:

setRequestProperty(String key, String value):设置该URLConnection的key请求头字段的值为value。如下代码所示:

public static String sendGet(String url , String param) 
{
String result = "";
BufferedReader in = null;
try
{
String urlName = url + "?" + param;
URL realUrl = new URL(urlName);
//打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
//设置通用的请求属性
conn.setRequestProperty("accept", "* 
public static String sendPost(String url,String param)
{
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try
{
URL realUrl = new URL(url);
//打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
//设置通用的请求属性
conn.setRequestProperty("accept", "*/*"); 
conn.setRequestProperty("connection", "Keep-Alive"); 
conn.setRequestProperty("user-agent", 
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 
//发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
//获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
//发送请求参数
out.print(param);
//flush输出流的缓冲
out.flush();
//定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine())!= null)
{
result += "\n" + line;
}
}
catch(Exception e)
{
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
return result;
}
//提供主方法,测试发送GET请求和POST请求
public static void main(String args[])
{
//发送GET请求
String s = TestGetPost.sendGet("http://localhost:8888/abc/
login.jsp",null);
System.out.println(s);
//发送POST请求
String s1 = TestGetPost.sendPost("http://localhost:8888/abc/a.jsp",
"user=李刚&pass=abc");
System.out.println(s1);
}
}

上面程序中发送GET请求时只需将请求参数放在URL字符串之后,以?隔开,程序直接调用URLConnection对象的connect方法即可,如程序中sendGet方法中粗体字代码所示;如果程序需要发送POST请求,则需要先设置doIn和doOut两个请求头字段的值,再使用URLConnection对应的输出流来发送请求参数即可,如程序中sendPost()方法中粗体字代码所示。


不管是发送GET请求,还是发送POST请求,程序获取URLConnection响应的方式完全一样:如果程序可以确定远程响应是字符流,则可以使用字符流来读取;如果程序无法确定远程响应是字符流,则使用字节流读取即可。
 

URLConnection 详细,布布扣,bubuko.com

URLConnection 详细

标签:style   blog   http   color   使用   width   

原文地址:http://www.cnblogs.com/helloxiaoxiang/p/3822811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!