HTTPClient

来源:互联网 发布:java多久可以学会 编辑:程序博客网 时间:2024/06/15 08:40
httpclient-4.0.1.jar
httpcore-4.0.1.jar
httpmime-4.0.1.jar- 又依赖于 mime4j(apache-mime4j-0.6.jar)
commons-codec-1.4.jar
commons-logging-1.1.1.jar

commons-io-1.4.jar – 为了更方便处理与 IO 有关的需求


2.1  最简单的获取网页内容的示例

try {String urlString = "你的链接";URL url = new URL(urlString); //代表了一个网址InputStream is = url.openStream(); //获得网页的内容//将InputStream转换为Reader,并使用缓冲读取,提高效率,同时可以按行读取内容BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));String line = null;while((line = br.readLine()) != null){System.out.println(line);}is.close();} catch (Exception e) {e.printStackTrace();}


2.2  URLConnection 的基本用法,设置代理

public void testFetch02(){try {String urlString ="你的请求地址";URL url = new URL(urlString); //代表了一个网址//首先创建HTTP代理,指定代理的地址和端口Proxy proxy = new Proxy(Proxy.Type.HTTP,newInetSocketAddress("79.120.193.53",80));/*** 首先打开一个连接对象* 可以通过这个对象,在真正发起请求之前,设置一些其它的信息* 比如:代理服务器等*/URLConnection conn = url.openConnection(proxy);InputStream is = conn.getInputStream(); //获得网页的内容//将InputStream转换为Reader,并使用缓冲读取,提高效率,同时可以按行读取内容BufferedReader br = new BufferedReader(newInputStreamReader(is,"UTF-8"));String line = null;while((line = br.readLine()) != null){System.out.println(line);}is.close();} catch (Exception e) {e.printStackTrace();}2.3  HttpURLConnection 的用法HttpURLConnection 是 URLConnection 的子类,它提供了更多与 HTTP 有关的处理方法、
try {String urlString ="http://localhost:8080/cms/backend/main.jsp";URL url = new URL(urlString); //代表了一个网址//设置是否自动进行重定向,缺省这个值为trueHttpURLConnection.setFollowRedirects(false);HttpURLConnection conn =(HttpURLConnection)url.openConnection();//设置HTTP METHODconn.setRequestMethod("GET");int code = conn.getResponseCode();System.out.println("服务器响应代码为:"+code);InputStream is = conn.getInputStream();//将InputStream转换为Reader,并使用缓冲读取,提高效率,同时可以按行读取内容BufferedReader br = new BufferedReader(newInputStreamReader(is,"UTF-8"));String line = null;while((line = br.readLine()) != null){System.out.println(line);}is.close();} catch (Exception e) {e.printStackTrace();}

3.  使用 HttpClient 获取网页内容

3.1使用GET方式向后台递交请求

public void testFetch01(){try {//HttpClient主要负责执行请求HttpClient httpclient = new DefaultHttpClient();//利用HTTP GET向服务器发起请求HttpGet get = new HttpGet("http://localhost:8080/cms");//获得服务器响应的的所有信息HttpResponse response = httpclient.execute(get);//获得服务器响应回来的消息体(不包括HTTP HEAD)HttpEntity entity = response.getEntity();if(entity != null){InputStream is = entity.getContent();//将InputStream转换为Reader,并使用缓冲读取,提高效率,同时可以按行读取内容BufferedReader br = new BufferedReader(newInputStreamReader(is,"UTF-8"));String line = null;while((line = br.readLine()) != null){System.out.println(line);}is.close();}//释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放httpclient.getConnectionManager().shutdown();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}

3.2自动获得响应的编码信息

public void testFetch02(){try {//HttpClient主要负责执行请求HttpClient httpclient = new DefaultHttpClient();//利用HTTP GET向服务器发起请求HttpGet get = new HttpGet("http://www.baidu.com/");//newHttpGet("http://localhost:8080/cms");//获得服务器响应的的所有信息HttpResponse response = httpclient.execute(get);//获得服务器响应回来的消息体(不包括HTTP HEAD)HttpEntity entity = response.getEntity();if(entity != null){//获得响应的字符集编码信息//即获取HTTP HEAD的:Content-Type:text/html;charset=UTF-8中的字符集信息String charset =EntityUtils.getContentCharSet(entity);System.out.println("响应的字符集是:"+charset);InputStream is = entity.getContent();//使用响应中的编码来解释响应的内容BufferedReader br = new BufferedReader(newInputStreamReader(is,charset));String line = null;while((line = br.readLine()) != null){System.out.println(line);}is.close();}//释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放httpclient.getConnectionManager().shutdown();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}

3.3设置代理服务器,访问网站

public void testFetch03(){try {//HttpClient主要负责执行请求HttpClient httpclient = new DefaultHttpClient();//设置代理服务器httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, newHttpHost("121.12.249.207",3128));//利用HTTP GET向服务器发起请求HttpGet get = new HttpGet("http://www.baidu.com/");//newHttpGet("http://localhost:8080/cms");//获得服务器响应的的所有信息HttpResponse response = httpclient.execute(get);//获得服务器响应回来的消息体(不包括HTTP HEAD)HttpEntity entity = response.getEntity();if(entity != null){//获得响应的字符集编码信息//即获取HTTP HEAD的:Content-Type:text/html;charset=UTF-8中的字符集信息String charset =EntityUtils.getContentCharSet(entity);System.out.println("响应的字符集是:"+charset);InputStream is = entity.getContent();//使用响应中的编码来解释响应的内容BufferedReader br = new BufferedReader(newInputStreamReader(is,charset));String line = null;while((line = br.readLine()) != null){System.out.println(line);}is.close();}//释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放httpclient.getConnectionManager().shutdown();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}


3.4获得重定向之后的网址信息

HttpClient 缺省情况下自动处理客户端重定向,即当你访问网页(比如 A 网页)之后,假设被重定向到了
B 网页,那么,HttpClient 将自动返回 B 网页的内容,无需再编程处理它!有时候我们可能想要知道 A 网
页被重定向到了哪里,也就是取得 B 网页的网址,那么可以通过下述例子获得:

public void testFetch04(){try {//HttpClient主要负责执行请求HttpClient httpclient = new DefaultHttpClient();HttpContext context = new BasicHttpContext();//利用HTTP GET向服务器发起请求HttpGet get = newHttpGet("http://localhost:8080/cms/backend/main.jsp");//获得服务器响应的的所有信息HttpResponse response = httpclient.execute(get,context);//获得重定向之后的主机地址信息HttpHost targetHost =(HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);System.out.println(targetHost); // http://localhost:8080//获得实际的请求对象的URI(即重定向之后的"/cms/backend/login.jsp")HttpUriRequest actualRequest = (HttpUriRequest)context.getAttribute(ExecutionContext.HTTP_REQUEST);System.out.println(actualRequest.getURI());//获得服务器响应回来的消息体(不包括HTTP HEAD)HttpEntity entity = response.getEntity();if(entity != null){//获得响应的字符集编码信息//即获取HTTP HEAD的:Content-Type:text/html;charset=UTF-8中的字符集信息String charset =EntityUtils.getContentCharSet(entity);System.out.println("响应的字符集是:"+charset);InputStream is = entity.getContent();//使用响应中的编码来解释响应的内容BufferedReader br = new BufferedReader(newInputStreamReader(is,charset));String line = null;while((line = br.readLine()) != null){System.out.println(line);}is.close();}//释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放httpclient.getConnectionManager().shutdown();} catch (Exception e) {e.printStackTrace();}}

3.5自动Cookie处理

HttpClient 能够支持自动 Cookie 处理。设想一个典型的场景:首先打开登录页面,然后输入用户名和密
码登录,然后访问那些只有登录之后才能访问的网页……
如果我们用浏览器,因为浏览器可以将登录之后的会话信息用 Cookie 存储在本地,所以,登录之后的每次
请求,都会自动向服务器发送 Cookie 的信息,我们利用 HttpClient,这些过程都全部可以自动化处理
了。

public void testFetch05(){try {//HttpClient主要负责执行请求HttpClient httpclient = new DefaultHttpClient();HttpContext context = new BasicHttpContext();//利用HTTP GET向服务器发起请求,HttpGet get = newHttpGet("http://localhost:8080/cms/backend/login.jsp");//获得服务器响应的的所有信息HttpResponse response = httpclient.execute(get,context);//获得服务器响应回来的消息体(不包括HTTP HEAD)HttpEntity entity = response.getEntity();String charset = null;if(entity != null){//获得响应的字符集编码信息//即获取HTTP HEAD的:Content-Type:text/html;charset=UTF-8中的字符集信息charset = EntityUtils.getContentCharSet(entity);System.out.println("响应的字符集是:"+charset);InputStream is = entity.getContent();//使用响应中的编码来解释响应的内容BufferedReader br = new BufferedReader(newInputStreamReader(is,charset));String line = null;while((line = br.readLine()) != null){System.out.println(line);}is.close();}//************* 执行登录请求 ********************//HttpPost post = newHttpPost("http://localhost:8080/cms/backend/LoginServlet");//添加POST参数List<NameValuePair> nvps = new ArrayList<NameValuePair>();nvps.add(new BasicNameValuePair("username","admin"));nvps.add(new BasicNameValuePair("password","admin"));post.setEntity(new UrlEncodedFormEntity(nvps,charset));response = httpclient.execute(post);entity = response.getEntity();if(entity != null){InputStream is = entity.getContent();//使用响应中的编码来解释响应的内容BufferedReader br = new BufferedReader(newInputStreamReader(is,charset));String line = null;while((line = br.readLine()) != null){System.out.println(line);}is.close();}get = newHttpGet("http://localhost:8080/cms/backend/ArticleServlet");response = httpclient.execute(get);entity = response.getEntity();if(entity != null){InputStream is = entity.getContent();//使用响应中的编码来解释响应的内容BufferedReader br = new BufferedReader(newInputStreamReader(is,charset));String line = null;while((line = br.readLine()) != null){System.out.println(line);}is.close();}//释放所有的链接资源,一般在所有的请求处理完成之后,才需要释放httpclient.getConnectionManager().shutdown();} catch (Exception e) {e.printStackTrace();}}




阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我活了万年 都市长生一万年 修真万年归来全文阅读 万年仙尊在都市 万年仙尊都市 修真万万年 一眼万万年作品 修真万万年txt下载 都市之我活了万万年 黄厉万年厉农历 万年厉查询表 老黄厉日厉万年厉 江西万年 万年县 万年县属于哪个市 万年县公共资源交易中心 万年县人民医院 江西省万年县湖锦线改造 江西万年县 万年场 万年寺 万年寺住宿 峨眉山万年寺住宿 峨眉山万年寺附近住宿 万年松财富 万年松 相聚万年树 万年红 万年红植物 黄鹤楼万年红 万年红图片 万年红拖拉机 万年红对联纸 万年红图片与养殖方法 万年草 黄金万年草 薄雪万年草 蓝万年草 黄金万年草的养殖方法 万年青多久浇一次水 万年青图片及养殖方法