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
3.3设置代理服务器,访问网站
3.5自动Cookie处理
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();}}
阅读全文