首页 > 其他分享 >业务接口造数据(httpclient)

业务接口造数据(httpclient)

时间:2023-04-23 13:40:13浏览次数:37  
标签:http org 业务 接口 client new apache import httpclient


导入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=ACE
	private 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=ACE
	private static String uriPOST = "http://localhost:9000/computers";

	public static void main(String[] args) {

		try {
			for (int i = 0; i < 10; i++) {
				CloseableHttpClient httpclient = HttpClients.createDefault();
				HttpPost httpPost = new HttpPost(uriPOST);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("name", "test" + 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 = System.currentTimeMillis();
				CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
				System.out.println((System.currentTimeMillis()-startTime) + "ms");
//				System.out.println(httpResponse.getStatusLine().getStatusCode());
				// 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.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;

/***
 * 接口测试四大步骤: 1 数据准备 2 测试执行 3 数据校验 检查 4 数据清洗
 * 
 * DatabaseComputer Server Httpclient request 强制使用这种fang
 * 
 * @author Administrator
 *
 */
public class HttpclientForDatabaseComputerPost2 {

	// send url is http://localhost:9000/computers?f=ACE
	private static String uriPOST = "http://localhost:9000/computers";

	public static int sendPostRequest(String ThreadName) throws ClientProtocolException, IOException {

		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(uriPOST);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		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 = System.currentTimeMillis();
		CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
		int responseCode = httpResponse.getStatusLine().getStatusCode();
		System.out.println(responseCode + " ," + (System.currentTimeMillis() - startTime) + "ms" + "  ," + ThreadName);
		return responseCode;
	}

	public static void main(String[] args) {
		// 5个线程 跑1分钟
		final long startTime = System.currentTimeMillis();

		for (int i = 0; i < 5; i++) {
			Thread t = new Thread(new Runnable() {
				AtomicBoolean flag = new AtomicBoolean(true);

				public void run() {
					while (flag.get()) {
						try {
							if ((System.currentTimeMillis() - startTime) >= 60 * 1000) {
								flag.set(false);
							}
							sendPostRequest(Thread.currentThread().getName());
						} catch (ClientProtocolException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			});
			t.start();
		}
	}
}







标签:http,org,业务,接口,client,new,apache,import,httpclient
From: https://blog.51cto.com/u_16084838/6217439

相关文章

  • HashSet以及TreeSet实现了哪些接口?有什么作用?
    HashSet简介HashSet是Java集合框架中非常常用的一种无序、不可重复的集合。它是通过哈希表来实现的,可以快速检索元素并消除重复。泛型的作用泛型可以帮助我们在编译时就发现类型错误,从而减少了运行时错误的发生。在使用HashSet时,我们通常会指定它的泛型类型为某个具体的类或接......
  • rails的接口查询详解
    RetrievingObjectsfromtheDatabasefind"find"是一种常用的数据库查询方法,在Rails中被用于从数据库中查找单个记录。它可以接收一个主键作为参数,也可以接收一组条件参数。以下是"find"方法的使用方式:#使用主键查找单个记录Model.find(1)#使用条件参数查找单个记录Mod......
  • 容易忽视的细节:Log4j 配置导致的零点接口严重超时
    作者:vivo互联网服务器团队-JiangYe本文详细的记录了一次0点接口严重超时的问题排查经历。本文以作者自身视角极具代入感的描绘了从问题定位到具体的问题排查过程,并通过根因分析并最终解决问题。整个过程需要清晰的问题排查思路和丰富的问题处理经验,也离不开公司强大的调用链......
  • Vue Typescript 引入文件接口,就无法使用withDefaults
    就是代码写的不规范报错写法 import{Setting}from'@element-plus/icons-vue' import{defineProps,withDefaults}from'vue' import{PiProject}from'@/types/Project' interfaceProjectCardProps{ project:PiProject } constprops=de......
  • 常用代码-接口交互+构建页面
    点击#search,无参请求,构造表格代码:functiongetDataSet(){$("body").on("click","#search",function(){$.ajax({type:"get",url:"http://114.67.241.121:8080/product/list",......
  • Java接口
    Java接口Java接口的概述接口是一种公共的规范标准只要符合规范标准,就可以给大家通用生活举例接口的定义和基本格式接口是多个类的公共规范接口是一种引用数据类型,里面最重要的方法是抽象方法接口的格式publicinterface接口名称{接口内容}接口可以包含常量......
  • ZLMediaKit实现按需拉流时rtsp流地址不对addStreamProxy返回0,接口流id参数踩坑记录
    场景开源流媒体服务器ZLMediaKit在Windows上运行、配置、按需拉流拉取摄像头rtsp视频流)并使用http-flv网页播放:开源流媒体服务器ZLMediaKit在Windows上运行、配置、按需拉流拉取摄像头rts基于上面实现拉取视频流预览时,发现当调用api传参时如果更换了rtsp视频流地址,但是没有更改流......
  • Forest-声明式HTTP客户端框架-集成到SpringBoot实现调用第三方restful api并实现接口
    场景Forest声明式HTTP客户端API框架,让Java发送HTTP/HTTPS请求不再难。它比OkHttp和HttpClient更高层,是封装调用第三方restfulapiclient接口的好帮手,是retrofit和feign之外另一个选择。通过在接口上声明注解的方式配置HTTP请求接口。官网:Forest 代码地址:forest:声明式HTTP客户......
  • 冰橙GPT提供开放接口 。提供与OPENAI官方一致的体验效果(同步返回数据,同时支持流式及非
    冰橙GPTchatGPT开放接口使用说明 【接入了腾讯云内容安全检测】冰橙GPT稳定提供API接口服务定时有人进行问题排查处理1小时内问题响应接入了腾讯云的内容安全检测有任何疑问请加入QQ交流群:310872519           1.请求地址:https://gpt.bcwhkj.cn/a......
  • SRE接手新业务首要工作:运维准入测试
    如果接手的是一坨随时可能散架的破车,就算SRE有通天之能,也很难通过运维手段给变成布加迪威龙。接手的时候一定要做好准入测试!很多公司会有运维准入规范,但是通常缺少运维准入测试,导致了后续诸多背锅问题。前言你可能会遇到下面的问题:告诉研发做架构设计的时候要叫上运维做review,研发......