HttpClient之初步认识与使用1

王朝学院·作者佚名  2016-05-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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方法,你会发现,我们能够跨项目获得字符串内容了~

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航