POST和GET方式的定义
HTTP-GET和HTTP-POST是使用HTTP的标准协议动词,用于编码和传送变量名/变量值对参数,并且使用相关的请求语义。
每个HTTP-GET和HTTP-POST都由一系列HTTP请求头组成,这些请求头定义了客户端从服务器请求了什么,而响应则是由一系列HTTP请求数据和响应数据组成,如果请求成功则返回响应的数据。
HTTP-GET以使用MIME类型application/x-www-form-urlencoded的urlencoded文本的格式传递参数,Urlencoding是一种字符编码,保证被传送的参数由遵循规范的文本组成,例如一个空格的编码是“%20”,附加参数还能被认为是一个查询字符串。
与HTTP-GET类似,HTTP-POST参数也是被URL编码的,然而,变量名/变量值不作为URL的一部分被传送,而是放在实际的HTTP请求消息内部被传送。
GET和POST之间的主要区别
1、GET是从服务器上获取数据,POST是向服务器传送数据
2、在客户端,GET方式在通过URL提交数据,数据在URL中可以看到,POST方式,数据防止在HTMLHEADER内提交
3、对于GET方式,服务端用Request.queryString获取变量的值,对于POST方式,服务端用Request.Form获取提交的数据
4、GET方式提交的数据最多只有1024字节,而POST则没有限制
5、安全性问题,正如2中提到的,使用GET的时候,参数会显示在地址栏上,而POST不会,所以这些数据是中文数据,而且是非敏感数据,那么使用GET,如果用户输入的数据不是中文字符,而是包含敏感数据,那么还是使用POST为好
URL的定义和组成
全称:Uniform Resource Locator 统一资源定位符
URL的组成部分:http://www.mbalib.com/china/index.htm
http://:代表超文本传输协议
www:代表一个万维网服务器
mbalib.com/:服务器的域名,或者服务器的名称
china/:子目录,类似于我们的文件夹
index.htm:是文件夹中的一个文件
/china/index.htm:统称为URL路径
java中HTTP编程接口
java中进行HTTP编程有如下的两种方式:
1、标准的java接口
从服务器上下载一张图片保存到本地:
public InputStream getInputStream(){
String path = "www.baidu.com";
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
URL url = new URL(path);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode ==200){
//从服务器获得一个输入流
inputStream = httpURLConnection.getInputStream();
}
return inputStream;
}
public void saveImageToDisk(){
//从服务器获得图片,保存到本地
InputStream inputStream = getInputStream();
byte[ ] data = new byte[1024];
int len = 0;
FileOutputStream fileOutputStream = new FileOutputStream("D:\\test.png");
try {
while( (inputStream.read(data)) !=-1){
fileOutputStream.write(data, 0, len);
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
if(inputStream !=null){
inputStream.close();
}
if(fileOutputStream !=null){
fileOutputStream.close();
}
}
}
2、标准的Apache接口
=从服务器获取数据的操作
public class HttpGetMethod {
private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
private InputStream inputStream = null;
public void onClick(View v){
//生成一个请求对象
HttpGet httpGet = new HttpGet("http//www.baidu.com");
//访问服务器可以是浏览器发送请求,也可以是其他的客户端
//此处生成一个客户端
HttpClient httpClient = new DefaultHttpClient();
//使用Http客户端发送请求对象
try {
//httpResponse 代表服务器端接受我们的请求后,返回给我们的响应
httpResponse = httpClient.execute(httpGet);
//服务器发回的响应内容,就是在httpEntity中
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while ((line = reader.readLine()) !=null){
result =result + line;
}
System.out.println(result);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
finally{
try{
inputStream.close();
} catch (Exception e){
e.printStackTrace();
}
}
}
}
/* ----使用httpClient访问步骤:
* 1、生成一个请求对象
* 2、生成一个http客户端对象
* 3、使用excute方法发送请求
* 4、得到响应,取出数据
*/
3、Http发送请求的方法
要实现这样一种功能,在Android设备上两个窗口,分别输入用户名和年龄,可以使用两种不同的方式向服务器提交请求
点击提交之后,服务器返回一段文字:我的名字叫zhangsan,我的年龄是20,对于服务器端的代码,我们暂时可以不用管它怎么实现的。
XML布局文件略……
=使用GET发送请求
public void methodGet(View v){
//和上次的GET方法不一样的是,我们不仅要向地址发送请求,还要携带数据发送过去
String name = nameView.getText().toString().trim();
String age = ageView.getText().toString().trim();
//记住:我们不仅仅要发送地址,还要向这个地址携带数据(姓名+年龄)
//例如:http://192.168.1.100:8081/serverside/name?name=zhangsan&age=20
<strong><span style="font-size:14px;color:#ff0000;">String url = baseUrl +"?" +"name=" + name + "&age=" + age;</span></strong>
//其他的和上边的代码都一样
/*
* <strong>此部分代码可以参考上边的GET请求方式代码,记得把url更换成下边的url就可以了 httpGet httpGet = ……</strong>
*/
}
=使用POST发送请求
private EditText nameView = null;
private EditText ageView = null;
private String baseUrl ="http://192.168.1.100:8081/serverside/name";
public void methodPost(View v) {
String name = nameView.getText().toString().trim();
String age = ageView.getText().toString().trim();
<strong><span style="color:#ff0000;">//发送一个键值对,把信息存入进去,键是服务器规定的
//我们把要发送的内容放入两个键值对中,再把键值对放入一个List中</span></strong>
NameValuePair nameValuePair1 = new BasicNameValuePair("name", name);
NameValuePair nameValuePair2 = new BasicNameValuePair("age", age);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(nameValuePair1);
nameValuePairs.add(nameValuePair2);
try { //httpEntity即可以表示请求的时候发送内容,也可以表示响应时候得到的内容
HttpEntity requestHttpEntity = new UrlEncodedFormEntity(nameValuePairs);
HttpPost httpPost = new HttpPost(baseUrl);
httpPost.setEntity(requestHttpEntity);
HttpClient httpClient = new DefaultHttpClient();
InputStream inputStream = null;
//下边这部分就是把服务器输出的内容显示出来
try {
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while ((line = reader.readLine()) !=null){
result =result + line;
}
System.out.println(result);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
finally{
try{
inputStream.close();
} catch (Exception e){
e.printStackTrace();
}
}
}
总结:
使用GET发送请求的时候和使用GET从网络获取内容的方式一样,只不过的是需要在后边加上“?”和键值对,发送到服务端;但是使用Post发送请求的时候,需要申请valuepare,把键和值都放进去,再把这两个放进List中,把list放进Entitiy里,再把entity放进Post,然后下边的就是解析从服务器上获取返回的值。