HttpClient4模拟表单提交[通俗易懂]

HttpClient4模拟表单提交[通俗易懂]这里用httpclient4.3模拟一个表单普通文本提交的方法建一个servlet接受表单数据,只传递2个参数,name和password//servlet的访问地址是:http://localhost:80/testjs/servlet/FormServletpublicclassFormServletextendsHttpServlet{publicvoidd

大家好,又见面了,我是你们的朋友全栈君。

这里用httpclient4.3模拟一个表单普通文本提交的方法

建一个servlet接受表单数据,只传递2个参数,name和password

//servlet的访问地址是:http://localhost:80/testjs/servlet/FormServlet

public class FormServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);

                //获取传递来的参数
String name = request.getParameter(“name”);
String password = request.getParameter(“password”);

System.out.println(“你输入的姓名是:”+name);
System.out.println(“你输入的密码是:”+password);
//设置响应内容
response.getWriter().write(name+”, 欢迎访问”);
}

}

用到的jar包有:commons-codec-1.6.jar,commons-logging-1.1.3.jar,httpclient-4.3.1.jar,httpcore-4.3.jar,httpmime-4.3.1.jar

package com.test.httpClient.myTest;

import java.io.IOException;

import java.nio.charset.Charset;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.Consts;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import org.junit.Test;

public class TestHttpClient4 {


@Test


public void test1() throws IOException{


CloseableHttpClient httpClient = HttpClients.createDefault();


try{


//post请求的url地址


HttpPost httpPost = new HttpPost(“http://localhost:80/testjs/servlet/FormServlet”);


List <NameValuePair> nvps = new ArrayList <NameValuePair>();


//传递2个参数  name和password


nvps.add(new BasicNameValuePair(“name”,”王五”));


nvps.add(new BasicNameValuePair(“password”,”12345″));


//转码  封装成请求实体


HttpEntity reqEntity = new UrlEncodedFormEntity(nvps,Consts.UTF_8);





httpPost.setEntity(reqEntity);





System.out.println(“请求url地址”+httpPost.getURI());


//提交表单请求   response是表单的响应


CloseableHttpResponse response = httpClient.execute(httpPost);

            try {

                HttpEntity respEntity = response.getEntity();

                //响应状态

                System.out.println(“Login form get: ” + response.getStatusLine());

                //EntityUtils.consume(entity);

                //获取响应内容

                System.out.println(EntityUtils.toString(respEntity,Charset.forName(“utf-8”)));

                //销毁

                EntityUtils.consume(respEntity);

            } finally {

                response.close();

            }


}finally{


httpClient.close();


}





}

}

运行结果 

test端

请求url地址http://localhost:80/testjs/servlet/FormServlet
Login form get: HTTP/1.1 200 OK
王五, 欢迎访问

tomcat服务器端

你输入的姓名是:王五
你输入的密码是:12345

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163293.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 解释afterPropertiesSet

    在spring的bean的生命周期中,实例化->生成对象->属性填充后会进行afterPropertiesSet方法,这个方法可以用在一些特殊情况中,也就是某个对象的某个属性需要经过外界得到,比如说查询数据库等方式,这时候可以用到spring的该特性,只需要实现InitializingBean即可:@Component(“a”)publicclassAimplementsInitializingBean{privateBb;publicA(Bb){

    2022年4月6日
    75
  • Python爬虫系列:爬取小说并写入txt文件

    Python爬虫系列:爬取小说并写入txt文件Python爬虫系列——爬取小说并写入txt文件文章介绍了如何从网站中爬取小说并写入txt文件中,实现了单章节写取,整本写取,多线程多本写取。爬虫使用的python版本为python3,有些系统使用python指令运行本脚本,可能出现错误,此时可以试一试使用python3运行本脚本。本文是一个教程,一步步介绍了如何爬取批量小说内容以及存储这是txt文件中,以下是项目源码地址。

    2022年5月29日
    72
  • Hibernate之Query接口的uniqueResult()方法[通俗易懂]

    Hibernate之Query接口的uniqueResult()方法[通俗易懂]如果查询返回多个值用list()方法public void testQuery(){Configuration config = new&#160

    2022年7月3日
    29
  • mysql的字符串拼接函数怎么用_拼接字段的函数是什么

    mysql的字符串拼接函数怎么用_拼接字段的函数是什么MySQL的字符串拼接有三个函数CONCAT(str1,str2,…)CONCAT_WS(separator,str1,str2,…)GROUP_CONCAT(expr)这三个函数都各有作用,现在测试看看是什么样子的效果准备数据表CREATETABLE`user_info`(`id`int(11)NOTNULLAUTO_INCREMENT,`name`varchar(255)DEFAULTNULL,`age`int(3)DEFAULTNULL,

    2025年7月13日
    2
  • 网管最看重的五种远程控制软件

    网管最看重的五种远程控制软件

    2021年7月31日
    71
  • Peeking inside LuaJIT(窥探LuaJIT)[通俗易懂]

    Peeking inside LuaJIT(窥探LuaJIT)[通俗易懂]原文链接:https://pwparchive.wordpress.com/2012/10/16/peeking-inside-luajit/LuaJITisahigh-performancevirtualmachineforLua.I’verecentlystartedplayingwithitandthisblogposttalksaboutwhat

    2022年9月29日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号