一、HttpClient简介
是一个开源的网络请求框架 在Android中是内置
|--但是:Android5.0之后该框架已经不建议使用了
二、第三方jar包,导包了,下载地址:
http://hc.apache.org/downloads.cgi
三、使用
1、创建客户端 HttpClient client = new DefaltHttpServlet();
2、创建请求HttpGet/HttpPost
3、使用客户端发送请求client.execute(请求) --返回一个reseponse对象
4、处理请求的结果
1、简单的代码,初级,中间有失效的方法就不要紧,异常没处理
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class HttpClientDemo01 {
public static void main(String[] args) throws Exception{
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/news/q=100/sign=643ba8b7d20735fa97f04ab9ae500f9f/8ad4b31c8701a18b48f518e3982f07082838fe04.jpg");
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
BufferedInputStream bis = new BufferedInputStream(entity.getContent());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a6.jpg"));
byte[] b = new byte[10240];
int len = 0;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
bos.flush();
bos.close();
bis.close();
} else {
System.out.println("臣妾做不到啊");
}
}
}
EntityUtils的使用:
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpClientDemo02 {
public static void main(String[] args) throws Exception{
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/news/q=100/sign=643ba8b7d20735fa97f04ab9ae500f9f/8ad4b31c8701a18b48f518e3982f07082838fe04.jpg");
HttpResponse responce = client.execute(get);
if (responce.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = responce.getEntity();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a7.jpg"));
//直接用工具转换城byte型数组
bos.write(EntityUtils.toByteArray(entity));
bos.close();
} else {
System.out.println("臣妾做不到啊");
}
}
}
提交表单和EntityUtils的使用:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* Entity的工具的使用和表单的提交
* @author Mixm
* @date 2015年10月8日 下午9:15:51
*/
public class HttpClientDemo03 {
public static void main(String[] args) throws Exception{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://127.0.0.1:8080/day028_web/LoginServlet");
//普通表单
NameValuePair name = new BasicNameValuePair("userName", "Mixm");
NameValuePair psw = new BasicNameValuePair("passWord", "0906");
List<NameValuePair> lists = new ArrayList<NameValuePair>();
lists.add(name);
lists.add(psw);
HttpEntity entity = new UrlEncodedFormEntity(lists);
post.setEntity(entity);
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity hEntity = response.getEntity();
System.out.println(EntityUtils.toString(hEntity));
} else {
System.out.println("臣妾做不到啊");
}
}
}
HttpPost和HttpGet的区别
如果只是从服务器取数据,就使用HttpGet
如果要向服务器传数据,就使用HttpPost