标签:
1. get是从服务器上获取数据,post是向服务器传送数据。
2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
3. get传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。
4. get安全性非常低,post安全性较高。但是执行效率却比Post方法好。
建议:
1、get方式的安全性较Post方式要差些,包含机密信息的话,建议用Post数据提交方式;
2、在做数据查询时,建议用Get方式;而在做数据添加、修改或删除时,建议用Post方式;
Request Headers Value
POST:
(Request-Line):POST /wdinfo.php HTTP/1.1
Host:qurl.f.360.cn
Accept:*/*
Cache-Control:no-cache
Content-Type:application/octet-stream
Content-Length:534
返回:响应头 值
(Status-Line) HTTP/1.1 200 OK
Cache-Control private
Date Mon, 16 Mar 2015 12:10:18 GMT
Expires Mon, 16 Mar 2015 12:10:18 GMT
Content-type text/html
Server BWS/1.0
Connection Keep-Alive
Content-Length 17
GET:
(Request-Line) GET /?s=20150316200919 HTTP/1.1
Accept image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
User-Agent Microsoft URL Control - 6.00.8169
Host www.baidu.com
Connection Keep-Alive
Cache-Control no-cache
Cookie BAIDUPSID=0EEE39AFCF8553059F84DD4BD6ED3E55; BAIDUID=C9C6C28B4741A72CB3850803AC4B651E:FG=1; BDUSS=hBY0NWV0tVbGFacGF3Z2lnZW5uQ1ZyZEpxd21xN05VNDBBVDh2R0Z5eDJ0UzFWQVFBQUFBJCQAAAAAAAAAAAEAAAA4B8QcREJaWkNaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHYoBlV2KAZVWU; H_PS_PSSID=8343_1439_12881_10902_10211_12575_12691_12694_12722_12735_12743_12781_11420_8498; BDSVRTM=221; BD_HOME=1; BD_UPN=112351; H_PS_BBANNER=5; H_PS_645EC=6439y0Fb0GTIsCg9fss%2FHiFv3rT%2BVoO%2B34IQ6USd5yLUR%2FMAYpol4WMWzhYSRxxEZWOQ
返回:响应头 值
(Status-Line) HTTP/1.1 200 OK
Date Mon, 16 Mar 2015 12:09:30 GMT
Content-Type text/html
Connection Keep-Alive
Vary Accept-Encoding
Cache-Control private
Expires Mon, 16 Mar 2015 12:09:30 GMT
Server BWS/1.1
BDPAGETYPE 2
BDQID 0xe7465d0d00000f35
BDUSERID 482608952
Set-Cookie BDSVRTM=193; path=/
Set-Cookie BD_HOME=1; path=/
Set-Cookie H_PS_PSSID=8343_1439_12881_10902_10211_12575_12691_12694_12722_12735_12743_12781_11420_8498; path=/; domain=.baidu.com
Content-Length 148892
一、*************************************************************************HttpPost方式:
// 第1步:创建HttpPost对象
HttpPost httpPost = new HttpPost(url);
// 设置HTTP POST请求参数必须用NameValuePair对象
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("bookname", etBookName
.getText().toString()));
// 设置HTTP POST请求参数
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 第2步:使用execute方法发送HTTP POST请求,并返回HttpResponse对象
httpResponse = new DefaultHttpClient().execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
// 第3步:使用getEntity方法获得返回结果
String result = EntityUtils.toString(httpResponse
.getEntity());
// 去掉返回结果中的“\r”字符,否则会在结果字符串后面显示一个小方格
tvQueryResult.setText(result.replaceAll("\r", ""));
}
****************************************************************************HttpGet方式:
String url = "http://10.197.214.222:8080/querybooks/QueryServlet";
// 向url添加请求参数
url += "?bookname=" + etBookName.getText().toString();
// 第1步:创建HttpGet对象
HttpGet httpGet = new HttpGet(url);
// 第2步:使用execute方法发送HTTP GET请求,并返回HttpResponse对象
httpResponse = new DefaultHttpClient().execute(httpGet);
// 判断请求响应状态码,状态码为200表示服务端成功响应了客户端的请求
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 第3步:使用getEntity方法获得返回结果
String result = EntityUtils.toString(httpResponse
.getEntity());
// 去掉返回结果中的“\r”字符,否则会在结果字符串后面显示一个小方格
tvQueryResult.setText(result.replaceAll("\r", ""));
}
二、HttpURLConnection方式:
@Override
public void onFileItemClick(String filename)
{
String uploadUrl = "http://192.168.17.104:8080/upload/UploadServlet";
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try
{
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
//设置HTTP请求方法,方法名必须大写,例如:POST,GET
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(
httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+ filename.substring(filename.lastIndexOf("/") + 1)
+ "\""
+ end);
dos.writeBytes(end);
FileInputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
while ((count = fis.read(buffer)) != -1)
{
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine();
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
dos.close();
is.close();
}
catch (Exception e)
{
setTitle(e.getMessage());
}
}
三、服务端返回代码:
response.setContentType("text/html;charset=utf-8");
String queryStr = "";
if ("post".equals(request.getMethod().toLowerCase()))
queryStr = "POST请求;查询字符串:" + new String(request.getParameter("bookname").getBytes(
"iso-8859-1"), "utf-8");
else if ("get".equals(request.getMethod().toLowerCase()))
queryStr = "GET请求;查询字符串:" + request.getParameter("bookname");
String s =queryStr
+ "[Java Web开发速学宝典;Java开发指南思想(第4版);Java EE开发宝典;C#开发宝典]";
PrintWriter out = response.getWriter();
out.println(s);
Content-Type对照表:
http://tool.oschina.net/commons
标签:
原文地址:http://www.cnblogs.com/Dbzzcz/p/4342809.html