/**
* 使用Http协议 以GET请求提交数据到服务器
* @param map 数据
* @param path 请求路径
* @return
* @throws Exception
*/
public static boolean doHttpGet(Map<String, String> map, String path) throws Exception{
//特点:http://localhost:8080/web/LoginServlet?name=itcast&pwd=234
//拼接uri
StringBuffer sb = new StringBuffer(path);
sb.append("?");
for(Map.Entry<String, String> entry:map.entrySet()){
String key = entry.getKey();
String value = entry.getValue();
sb.append(key).append("=").append(value);
sb.append("&");
}
//删除最后一个&
sb.deleteCharAt(sb.length()-1);
URL url = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if(conn.getResponseCode() == 200){
//获取服务器回送的流数据
InputStream inputStream = conn.getInputStream();
//返回 success or error
String content = changeStreamToString(inputStream);
if("success".equals(content)){
return true;
}else{
return false;
}
}
return false;
}
•(掌握)post方式提交数据到服务器不会把参数带在url的后面
把参数放在请求实体里面
请求头
POST /web/LoginServlet HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://localhost:8080/web/
Accept-Language: zh-CN
浏览器的内核信息
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.2)
提交的数据类型
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:8080
提交数据的长度
Content-Length: 19
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=0D35C904B671C07943E0B9F374F2A162
实体内容
name=itcast&pwd=123
服务器响应的信息
HTTP/1.1 200 OK 状态行 状态码
Server: Apache-Coyote/1.1
Content-Length: 7
Date: Mon, 19 Jan 2015 08:05:00 GMT
success
执行Post请求的要求:
1 HTTP协议 客户端默认是不能提交数据到服务器 必须手动的开启 允许 对外输出
2 请求头里面必须要有Content-Type 和 Content-Length属性
/**
* 使用Http协议 以POST请求提交数据到服务器
* @param map 数据
* @param path 请求路径
* @return
* @throws Exception
*/
public static boolean doHttpPost(Map<String, String> map, String path) throws Exception{
//特点:数据是在请求实体里面
//拼接实体 name=itcast&pwd=123
StringBuffer sb = new StringBuffer();
for(Map.Entry<String, String> entry:map.entrySet()){
String key = entry.getKey();
String value = entry.getValue();
sb.append(key).append("=").append(value);
sb.append("&");
}
//删除多余的&
sb.deleteCharAt(sb.length()-1);
//实体
byte[] entity = sb.toString().getBytes();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
//运行对外输出
conn.setDoOutput(true);
//请求参数
//提交数据的类型
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//实体的长度
conn.setRequestProperty("Content-Length", entity.length+"");
//把实体数据对外输出
OutputStream outputStream = conn.getOutputStream();
outputStream.write(entity);
if(conn.getResponseCode() == 200){
//获取服务器回送的流数据
InputStream inputStream = conn.getInputStream();
//返回 success or error
String content = changeStreamToString(inputStream);
if("success".equals(content)){
return true;
}else{
return false;
}
}
return false;
}
•(掌握)httpclient的get和postHttpClient:他是android集成的一个三方框架 Http框架
HttpClient在URL HttpClient再次进行了oop的封装
HttpClient:他就是一个浏览器
执行请求 回送响应
HttpClient:浏览器 方法:执行 响应 HttpReponse exeucute(请求HttpUriRequest)
/**
* 使用HttpClient框架 以GET请求提交数据到服务器
* @param map 数据
* @param path 请求路径
* @return
* @throws Exception
*/
public static boolean doHttpClientGet(Map<String, String> map, String path) throws Exception{
//特点:http://localhost:8080/web/LoginServlet?name=itcast&pwd=234
//拼接uri
StringBuffer sb = new StringBuffer(path);
sb.append("?");
for(Map.Entry<String, String> entry:map.entrySet()){
String key = entry.getKey();
String value = entry.getValue();
sb.append(key).append("=").append(value);
sb.append("&");
}
//删除最后一个&
sb.deleteCharAt(sb.length()-1);
//浏览器
HttpClient httpClient = new DefaultHttpClient();
//get请求对象
HttpGet request = new HttpGet(sb.toString());
//执行请求
HttpResponse response = httpClient.execute(request);
//判断响应码是否为200
if(response.getStatusLine().getStatusCode() == 200){
//获取服务器回送的流
InputStream inputStream = response.getEntity().getContent();
//返回 success or error
String content = changeStreamToString(inputStream);
if("success".equals(content)){
return true;
}else{
return false;
}
}
return false;
}
post请求
/**
* 使用HttpClient框架 以POST请求提交数据到服务器
* @param map 数据
* @param path 请求路径
* @return
* @throws Exception
*/
public static boolean doHttpClientPost(Map<String, String> map, String path) throws Exception{
//post请求:请求实体
HttpClient client = new DefaultHttpClient();//浏览器
HttpPost request = new HttpPost(path);//post请求对象
//实体参数
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
for(Map.Entry<String, String> entry:map.entrySet()){
String name = entry.getKey();
String value = entry.getValue();
NameValuePair nvp = new BasicNameValuePair(name, value);
parameters.add(nvp);
}
//实体
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
//把实体设置给请求对象
request.setEntity(entity);
//执行请求
HttpResponse response = client.execute(request);
//判断响应码是否为200
if(response.getStatusLine().getStatusCode() == 200){
//获取服务器回送的流
InputStream inputStream = response.getEntity().getContent();
//返回 success or error
String content = changeStreamToString(inputStream);
if("success".equals(content)){
return true;
}else{
return false;
}
}
return false;
}
作业:
下载资源
相关链接:
长沙中心--黑马双元课堂JAVA入学辅导班1期火爆开班啦!!!
长沙黑马程序员学习激情无限“吊炸天”