HttpClient 基本功能的使用
HttpClient 功能介绍
HttpClient 提供的主要的功能,实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
1---->支持自动转向
2---->支持 HTTPS 协议
3---->支持代理服务器等
HttpClient 基本功能的使用
GET 方法
使用 HttpClient 需要以下 6 个步骤:
1. 创建 HttpClient 的实例
2. 创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址
3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
4. 读 response
5. 释放连接。无论执行方法是否成功,都必须释放连接
6. 对得到后的内容进行处理
根据以上步骤,我们来编写用GET方法来取得某网页内容的代码。
大部分情况下 HttpClient 默认的构造函数已经足够使用。 HttpClient httpClient = new HttpClient();
创建GET方法的实例。在GET方法的构造函数中传入待连接的地址即可。用GetMethod将会自动处理转发过程,如果想要把自动处理转发过程去掉的话,可以调用方法setFollowRedirects(false)。 GetMethod getMethod = new GetMethod("http://www.ibm.com/");
调用实例httpClient的executeMethod方法来执行getMethod。由于是执行在网络上的程序,在运行executeMethod方法的时候,需要处理两个异常,分别是HttpException和IOException。引起第一种异常的原因主要可能是在构造getMethod的时候传入的协议不对,比如不小心将"http"写成"htp",或者服务器端返回的内容不正常等,并且该异常发生是不可恢复的;第二种异常一般是由于网络原因引起的异常,对于这种异常 (IOException),HttpClient会根据你指定的恢复策略自动试着重新执行executeMethod方法。HttpClient的恢复策略可以自定义(通过实现接口HttpMethodRetryHandler来实现)。通过httpClient的方法setParameter设置你实现的恢复策略,本文中使用的是系统提供的默认恢复策略,该策略在碰到第二类异常的时候将自动重试3次。executeMethod返回值是一个整数,表示了执行该方法后服务器返回的状态码,该状态码能表示出该方法执行是否成功、需要认证或者页面发生了跳转(默认状态下GetMethod的实例是自动处理跳转的)等。 //设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); //执行getMethodint statusCode = client.executeMethod(getMethod);if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine());}
InputStream resStream = method.getResponseBodyAsStream(); BufferedReader br = new BufferedReader(new InputStreamReader(resStream)); StringBuffer resBuffer = new StringBuffer(); String resTemp = ""; while((resTemp = br.readLine()) != null){ resBuffer.append(resTemp); } String response = resBuffer.toString();
//private String postRequest(VehicleForm vehicleForm){//HttpClient httpClient = new HttpClient();//String ful = "";//GetMethod getMethod = new GetMethod("http://localhost.helloclient.net/Clogon?fsn="+vehicleForm.getFTerminalID());//// 执行postMethod//try {////设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略//getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, // new DefaultHttpMethodRetryHandler()); ////执行getMethod//int statusCode = httpClient.executeMethod(getMethod);//if (statusCode != HttpStatus.SC_OK) {// System.err.println("Method failed: " + getMethod.getStatusLine());//}//byte[] responseBody = getMethod.getResponseBody();//ful=new String(responseBody);//} catch (HttpException de) {//System.out.println("传入协议异常:" + de.toString());//} catch (IOException e) {//System.out.println("网络异常:" + e.toString());//} finally {//// 释放连接//getMethod.releaseConnection();//}//return ful;//}
public class Clogon extends HttpServlet { Terminal te;public void destroy() {super.destroy(); // Put your code here}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();//if(te.terminalIs(request.getParameter("fsn")))//out.print("ok");//else//out.print("error");out.print("ok");out.flush();out.close();} public void init() throws ServletException {// Put your code herete = new Terminal();} }