您的位置:首页 > 理论基础 > 计算机网络

HttpUrlConnection的简单使用--get和post的简单使用

2016-04-15 12:56 567 查看
URL请求的类别分为二类,GET与POST请求。二者的区别在于:

1、get请求可以获取静态页面,也可以把参数放在URL字串后面进行传递。

2、post与get的不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。

来一张图,大概了解一下HttpUrlConnection的使用流程。



接下来,分别上get和post的代码:

GET请求:

public void httpUrlConnetionGet() {
try {
//创建URL对象
URL url = new URL("http://www.baidu.com");//Get请求可以在Url中带参数: ①url + "?bookname=" + name;②url="http://www.baidu.com?name=zhang&pwd=123";
//返回一个URLConnection对象,它表示到URL所引用的远程对象的连接
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//在这里设置一些属性,详细见UrlConnection文档,HttpURLConnection是UrlConnection的子类
//设置连接超时为5秒
httpURLConnection.setConnectTimeout(5000);
//设定请求方式(默认为get)
httpURLConnection.setRequestMethod("GET");
//建立到远程对象的实际连接
httpURLConnection.connect();
//返回打开连接读取的输入流,输入流转化为StringBuffer类型,这一套流程要记住,常用
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
//转化为UTF-8的编码格式
line = new String(line.getBytes("UTF-8"));
stringBuffer.append(line);
}
Log.e("Get请求返回的数据", stringBuffer.toString());
bufferedReader.close();
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}


POST请求:

public void httpUrlConnectionPost() {
try {
//创建URL对象
URL url = new URL("http://www.baidu.com");
//返回一个URLConnection对象,它表示到URL所引用的远程对象的连接
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//在这里设置一些属性,详细见UrlConnection文档,HttpURLConnection是UrlConnection的子类
//设置连接超时为5秒
httpURLConnection.setConnectTimeout(5000);
//设定请求方式(默认为get)
httpURLConnection.setRequestMethod("POST");
// 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true, 默认情况下是false;
httpURLConnection.setDoOutput(true);
// 设置是否从httpUrlConnection读入,默认情况下是true;
httpURLConnection.setDoInput(true);
// Post 请求不能使用缓存
httpURLConnection.setUseCaches(false);

//这边开始设置请求头
// 设定传送的内容类型是可序列化的java对象(如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
httpURLConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");
//方法setRequestProperty(String key, String value)设置一般请求属性。
// 连接,从上述url.openConnection()至此的配置必须要在connect之前完成,
httpURLConnection.connect();

//这边设置请内容
//getOutputStream()里默认就有connect()了,可以不用写上面的连接
//接下来我们设置post的请求参数,可以是JSON数据,也可以是普通的数据类型
OutputStream outputStream = httpURLConnection.getOutputStream();
/**
* JSON数据的请求
* outputStream.write(stringJson.getBytes(), 0, stringJson.getBytes().length);
* outputStream.close();
* **/
/**
* 字符串数据的请求
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
String content = "username=" + username + "&password=" + password;
dataOutputStream.writeBytes(content);
dataOutputStream.flush();
dataOutputStream.close();
* **/
//读取返回的数据
//返回打开连接读取的输入流,输入流转化为StringBuffer类型,这一套流程要记住,常用
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
//转化为UTF-8的编码格式
line = new String(line.getBytes("UTF-8"));
stringBuffer.append(line);
}
Log.e("POST请求返回的数据", stringBuffer.toString());
bufferedReader.close();
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: