HTTPClient4.3.5的示例

来源:互联网 发布:angularjs http json 编辑:程序博客网 时间:2024/05/16 10:37
package cn.lomoxy.crawler;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;


public class Demo1 {


/**
* 简单获取一个html页面
* 1.创建HttpClient
* 2.创建HttpGet,指定目标网址
* 3.执行返回response
* 4.通过response获取网页实体
* 5.关闭HttpClient流
* @throws ClientProtocolException
* @throws IOException
*/
@Test
public void getHTML(){
try {
CloseableHttpClient client=HttpClients.createDefault();
HttpGet get=new HttpGet("http://www.baidu.com");
HttpResponse response=client.execute(get);
HttpEntity entity=response.getEntity();
String html=EntityUtils.toString(entity);
// InputStream contentStream=entity.getContent();
// String content=IOUtils.toString(contentStream, "UTF-8");
client.close();
System.out.println(html);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 下载一个图片文件
* 注意:fos.write(byte)会失真
* 应该使用fos.write(byte,begin,end)
* @throws ClientProtocolException
* @throws IOException
*/
@Test
public void DownLoadFile() throws ClientProtocolException, IOException{
//指定图片地址
String url="http://ww2.sinaimg.cn/large/9d57a855jw1dqpv9fp4yuj.jpg";
String desFilePath="D:/cat.jpg";//存储图片地址

//创建HttpClient对象
CloseableHttpClient client=HttpClients.createDefault();
HttpGet get=new HttpGet(url);
HttpResponse response=client.execute(get);
HttpEntity entity=response.getEntity();
InputStream is=entity.getContent();

File file=new File(desFilePath);
FileOutputStream fos=new FileOutputStream(file);
int num=-1;
byte[] temp=new byte[1024];
while((num=is.read(temp))!=-1){
//若用write(buff),图片会失真
// fos.write(temp);
fos.write(temp, 0, num);
}
fos.flush();
fos.close();
client.close();
}


/**
* 模拟登录,以人人网为例
* @throws IOException 
* @throws ClientProtocolException 
*/
@Test
public void Login() throws ClientProtocolException, IOException{

CloseableHttpClient client=HttpClients.createDefault();;
HttpPost post=new HttpPost("http://www.renren.com/PLogin.do");//发送post请求,提交登录数据
HttpGet get;//获取登陆后的页面
String login_success;//用于构造HttpGet

//打包将要传入的参数
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", "x1988y1101@hotmail.com"));
params.add(new BasicNameValuePair("password", "319505renren"));
post.setEntity(new UrlEncodedFormEntity(params));

//提交登录数据
HttpResponse response=client.execute(post);
//获取跳转的网页
Header locationHeader=response.getFirstHeader("Location");
//登陆不成功
if(locationHeader==null){
System.out.println("登录失败");
return;
}else{
//登陆成功
login_success=locationHeader.getValue();//获取成功登录后的跳转链接
System.out.println("成功登录,跳转:"+login_success);
}

get=new HttpGet(login_success);
HttpResponse response2=client.execute(get);
//输出登陆后的页面
String page=EntityUtils.toString(response2.getEntity());
System.out.println(page);

post.abort();
get.abort();
client.close();
}

}
0 0