HttpClient之初步认识与使用1

今天在项目中看到HttpClient的内容,然后去看了一下资料,有了初步的见解,在此记录一下~

 

一. 搭建环境

  (1)创建一个java项目叫HttpClientTest

  (2)为了使用HttpClient的类,你需要导入一下jar包:

  

  (3)创建一个类,名叫Test1

 

二.HttpClient模拟http请求

Test1代码如下:

package com.vmaxtam;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class Test1 {

    public static void main(String[] args) throws Exception {
        //用于模拟客户端发送请求和接收响应
        HttpClient client = new HttpClient();
        
        // 使用 GET 方法 ,如果服务器需要通过 HTTPS 连接,那只需要将下面 URL 中的 http 换成 https
         HttpMethod method = new GetMethod("http://java.sun.com");
        // 使用POST方法
        //HttpMethod method = new PostMethod("http://java.sun.com");
         
         //发送请求
        client.executeMethod(method);

        // 打印服务器返回的状态行
        System.out.println(method.getStatusLine());
        
        System.out.println("-------------------------1");
        
        // 返回的信息
        System.out.println(method.getResponseBodyAsString());
        // 释放连接
        method.releaseConnection();
    }
}

  运行main方法后,你会发现,method里头封装了sun页面的html代码。。。。。

 

三. 连接自己的服务端

为了证明 二 中的观点,我们写一个自己的服务端,还有页面。

  (1)建立一个项目作为服务端,新建一个web项目,名字叫MyServer

  (2)WEB-INF 里头自动生成了 index.jsp

  (3)把MyServer部署在开发环境的服务器上,运行起来。

  (4)在刚才Test1的main方法中,修改声明method变量时作为构造方法参数字符串 :

    HttpMethod method = new GetMethod("http://localhost:8080/MyServer/index.jsp");

  (5)运行Test1中的main方法,你会发现,method.getResponseBodyAsString()返回的的确就是index.jsp的代码

 

四.连接到servlet,并返回特定的字符串

  (1)在刚才的MyServer项目中,新建一个Servlet,名叫Myservlet

  (2)编辑Myservlet,代码如下

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Myservlet
 */
public class Myservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().write("you are crazy!");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().write("Game Over!");
    }

}

  (3)别忘了在web.xml中配置这个servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <servlet>
        <description>
        </description>
        <display-name>Myservlet</display-name>
        <servlet-name>Myservlet</servlet-name>
        <servlet-class>servlet.Myservlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Myservlet</servlet-name>
        <url-pattern>/Myservlet</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

(4)在刚才Test1的main方法中,修改声明method变量时作为构造方法参数字符串 :

HttpMethod method = new GetMethod("http://localhost:8080/SSH/Myservlet");

(5)运行Test1的main方法,你会发现,我们能够跨项目获得字符串内容了~

posted @ 2015-07-01 15:02  白Rime  阅读(777)  评论(0编辑  收藏  举报