热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

业务接口造数据(httpclient)

导入httpclientjar包创建maven工程httpclient发送get请求packagecom.testing91;importjava.io.BufferedRead

导入httpclient jar包 创建maven工程 

httpclient 发送get请求

package com.testing91;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;/*** 接口测试四大步骤: 1 数据准备 2 测试执行 3 数据校验 检查 4 数据清洗* * DatabaseComputer Server Httpclient request 强制使用这种fang* * @author Administrator**/
public class HttpclientForDatabaseComputerGet {// send url is http://localhost:9000/computers?f=ACEprivate static String uri = "http://localhost:9000/computers?f=ACE";public static void main(String[] args) {try {CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpGet = new HttpGet(uri);CloseableHttpResponse httpResponse = httpclient.execute(httpGet);// 3 获取响应体数据流,然后放入缓冲区BufferedReader in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "utf-8"));StringBuffer sb = new StringBuffer("");String line = "";// 4 读取缓冲区内容为字符串while ((line = in.readLine()) != null) {sb.append(line + "\n");}// 5 关闭缓冲区in.close();String content = sb.toString();// 6 测试校验System.out.println(content);} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}
}


httpclient  发送post请求单线程

package com.testing91;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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;/**** 接口测试四大步骤: 1 数据准备 2 测试执行 3 数据校验 检查 4 数据清洗* * DatabaseComputer Server Httpclient request 强制使用这种fang* * @author Administrator**/
public class HttpclientForDatabaseComputerPost {// send url is http://localhost:9000/computers?f&#61;ACEprivate static String uriPOST &#61; "http://localhost:9000/computers";public static void main(String[] args) {try {for (int i &#61; 0; i <10; i&#43;&#43;) {CloseableHttpClient httpclient &#61; HttpClients.createDefault();HttpPost httpPost &#61; new HttpPost(uriPOST);List params &#61; new ArrayList();params.add(new BasicNameValuePair("name", "test" &#43; i));params.add(new BasicNameValuePair("introduced", "2017-2-19"));params.add(new BasicNameValuePair("discontinued", "2017-2-19"));params.add(new BasicNameValuePair("company", "22"));httpPost.setEntity(new UrlEncodedFormEntity(params));long startTime &#61; System.currentTimeMillis();CloseableHttpResponse httpResponse &#61; httpclient.execute(httpPost);System.out.println((System.currentTimeMillis()-startTime) &#43; "ms");
// System.out.println(httpResponse.getStatusLine().getStatusCode());// &#xff13; 获取响应体数据流&#xff0c;然后放入缓冲区BufferedReader in &#61; new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "utf-8"));StringBuffer sb &#61; new StringBuffer("");String line &#61; "";// &#xff14; 读取缓冲区内容为字符串while ((line &#61; in.readLine()) !&#61; null) {sb.append(line &#43; "\n");}// 5 关闭缓冲区in.close();String content &#61; sb.toString();// &#xff16; 测试校验// System.out.println(content);}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}
}


httpclient  发送post请求 多线程

package com.testing91;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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;/**** 接口测试四大步骤&#xff1a; &#xff11; 数据准备 &#xff12; 测试执行 &#xff13; 数据校验 检查 &#xff14; 数据清洗* * DatabaseComputer Server Httpclient request 强制使用这种fang* * &#64;author Administrator**/
public class HttpclientForDatabaseComputerPost2 {// send url is http://localhost:9000/computers?f&#61;ACEprivate static String uriPOST &#61; "http://localhost:9000/computers";public static int sendPostRequest(String ThreadName) throws ClientProtocolException, IOException {CloseableHttpClient httpclient &#61; HttpClients.createDefault();HttpPost httpPost &#61; new HttpPost(uriPOST);List params &#61; new ArrayList();params.add(new BasicNameValuePair("name", "test"));params.add(new BasicNameValuePair("introduced", "2017-2-19"));params.add(new BasicNameValuePair("discontinued", "2017-2-19"));params.add(new BasicNameValuePair("company", "22"));httpPost.setEntity(new UrlEncodedFormEntity(params));long startTime &#61; System.currentTimeMillis();CloseableHttpResponse httpResponse &#61; httpclient.execute(httpPost);int responseCode &#61; httpResponse.getStatusLine().getStatusCode();System.out.println(responseCode &#43; " ," &#43; (System.currentTimeMillis() - startTime) &#43; "ms" &#43; " ," &#43; ThreadName);return responseCode;}public static void main(String[] args) {// &#xff15;个线程 跑&#xff11;分钟final long startTime &#61; System.currentTimeMillis();for (int i &#61; 0; i <5; i&#43;&#43;) {Thread t &#61; new Thread(new Runnable() {AtomicBoolean flag &#61; new AtomicBoolean(true);public void run() {while (flag.get()) {try {if ((System.currentTimeMillis() - startTime) >&#61; 60 * 1000) {flag.set(false);}sendPostRequest(Thread.currentThread().getName());} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}});t.start();}}
}









推荐阅读
  • 本文介绍了解决Netty拆包粘包问题的一种方法——使用特殊结束符。在通讯过程中,客户端和服务器协商定义一个特殊的分隔符号,只要没有发送分隔符号,就代表一条数据没有结束。文章还提供了服务端的示例代码。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • Skywalking系列博客1安装单机版 Skywalking的快速安装方法
    本文介绍了如何快速安装单机版的Skywalking,包括下载、环境需求和端口检查等步骤。同时提供了百度盘下载地址和查询端口是否被占用的命令。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • Android工程师面试准备及设计模式使用场景
    本文介绍了Android工程师面试准备的经验,包括面试流程和重点准备内容。同时,还介绍了建造者模式的使用场景,以及在Android开发中的具体应用。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • Ihaveapolynomial(generatedfromthecharacteristicpolynomialofamatrix)andIdliketosolve ... [详细]
  • 上图是InnoDB存储引擎的结构。1、缓冲池InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理。因此可以看作是基于磁盘的数据库系统。在数据库系统中,由于CPU速度 ... [详细]
  • 本文介绍了使用数据库管理员用户执行onstat -l命令来监控GBase8s数据库的物理日志和逻辑日志的使用情况,并强调了对已使用的逻辑日志是否及时备份的重要性。同时提供了监控方法和注意事项。 ... [详细]
  • 本文介绍了在sqoop1.4.*版本中,如何实现自定义分隔符的方法及步骤。通过修改sqoop生成的java文件,并重新编译,可以满足实际开发中对分隔符的需求。具体步骤包括修改java文件中的一行代码,重新编译所需的hadoop包等。详细步骤和编译方法在本文中都有详细说明。 ... [详细]
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
  • OpenMap教程4 – 图层概述
    本文介绍了OpenMap教程4中关于地图图层的内容,包括将ShapeLayer添加到MapBean中的方法,OpenMap支持的图层类型以及使用BufferedLayer创建图像的MapBean。此外,还介绍了Layer背景标志的作用和OMGraphicHandlerLayer的基础层类。 ... [详细]
  • Jboss的EJB部署描述符standardjaws.xml配置步骤详解
    本文详细介绍了Jboss的EJB部署描述符standardjaws.xml的配置步骤,包括映射CMP实体EJB、数据源连接池的获取以及数据库配置等内容。 ... [详细]
author-avatar
泉水叮咚139
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有