使用HttpClient实现接口测试

这篇博客记录了使用HttpClient4.2.5进行接口测试的过程,测试结果返回JSON格式的数据,包括商品信息如ID、名称、图片等。博主参考了两篇相关文章来解析JSON数据,提供了链接供后续查阅。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用HttpClient4.2.5做了一个简单的接口测试的实验,记录下来以后参考。

package cn.test;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class MobileLibInterfaceTest {
	private static final int SUCCESS_CODE = 1;
	private static final String PRODUCT_ID = "326610";
	private static final String PRODUCT_NAME = "三星 GALAXY Note II N7100(16GB)";
	//初始化httpClient
	HttpClient httpClient = new DefaultHttpClient();
	
	@BeforeClass
	public void beforeClass() {
	}
	
	@Parameters({"URL"})
	@Test 
	public void productInfo(String URL) throws ClientProtocolException, IOException, JSONException {
		//获取httpGet
		HttpGet httpGet = new HttpGet(URL);
		//执行get请求
		HttpResponse response = httpClient.execute(httpGet);
		//判断服务器响应的状态码
		System.out.println(response.getStatusLine().getStatusCode());
		Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode(),"状态码不是200,请求没成功");
		//读取返回数据
		String entity = EntityUtils.toString(response.getEntity());
		//返回的数据类型为json,使用org.json jar包来解析
		JSONObject json = new JSONObject(entity);
		System.out.println(json.get("code"));
		Assert.assertEquals(SUCCESS_CODE, json.get("code"),"返回的code不为1,请求失败");
		//返回的结果中,data为数组,将其转换为list,获取指定key的值
		JSONArray array = json.getJSONArray("data");
		for(int i=0; i<array.length(); i++) {
			JSONObject data = array.getJSONObject(i);
			String phoneID = data.getString("id");
			Assert.assertEquals(phoneID, PRODUCT_ID, "返回的产品 型号ID不一致");
			String phoneName = data.getString("name");
			Assert.assertEquals(phoneName, PRODUCT_NAME, "返回的产品名称不一致");
			System.out.println(phoneID+", "+phoneName);
		}
	}
	@AfterClass
	public void afterClass() {
		//断开连接
		httpClient.getConnectionManager().shutdown();
	}
}


请求成功后,页面返回的是的json格式的数据

{"code":1,"data":[{"id":"326610","name":"\u4e09\u661f GALAXY Note II N7100\uff0816GB\uff09","picture":"http:\/\/y1.ifengimg.com\/iproduct\/p1\/pics_big\/240\/ceK6fPqR05Yp6_top.jpg","marketPrice":"4699","score":"9","marketTime":"1349020800","mobileStyle":"3G\u624b\u673a\uff0c\u667a\u80fd\u624b\u673a\uff0c\u62cd\u7167\u624b\u673a\uff0c4G\u624b\u673a\uff0c\u5e73\u677f\u624b\u673a","screenSize":"5.500","isTouchScreen":"\u7535\u5bb9\u5c4f\uff0c\u591a\u70b9\u89e6\u63a7","screeType":"HD Super AMOLED","screeLong":"1280x720\u50cf\u7d20","networkType":"\u5355\u5361\u591a\u6a21","GSM":"GSM\uff0cWCDMA\uff0cLTE","dataService":"GPRS\uff0cEDGE\uff0cHSPA","supportfrequency":"2G\uff1aGSM 850\/900\/1800\/1900\r\n3G\uff1aWCDMA 850\/900\/1900\/2100","KeyboardType":"\u865a\u62dfQWERTY\u952e\u76d8","color":"\u4e91\u77f3\u767d\u8272\uff0c\u949b\u91d1\u7070\u8272\uff0c\u94bb\u77f3\u7c89\u8272","long":"151.1x80.5x9.4mm","weight":"180g"}],"msg":null}

最后需要解析json的数据,参考了下面这两篇文章:

http://blog.chinaunix.net/uid-25885064-id-3402449.html

http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/23/3096001.html

接口名称

手机对比

接口示例

http://digi.tech.ifeng.com/mobile/compare/api?ids=id1_id2_id3

接口描述

使用手机id进行参数对比

接口协议

HTTP[POST]

参数说明

ids

要对比的手机id序列,用下划线_分隔

必填

返回值约定(json格式)

注册失败时,显示相应的失败信息

成功时:{"code":1,"message":"\u64cd\u4f5c\u6210\u529f","data":["id1":{“name”:”iphone5”,”price”:4570,”pic”: “http://y1.ifengimg.com/iproduct/p1/top/pics/753/ceLBceL0LElec_top.jpg ”},“id2”:{“name”: “iphone5”, “price”:4570, “pic”: “http://y1.ifengimg.com/iproduct/p1/top/pics/753/ceLBceL0LElec_top.jpg ”},“id3”:{“name”: “iphone5”, “price”:4570, “pic”: “http://y1.ifengimg.com/iproduct/p1/top/pics/753/ceLBceL0LElec_top.jpg”}]}

备注

返回数据说明

code:接口调用结果 0-失败 1-成功

message:错误信息

data:手机信息数组  包括手机id和多个属性

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值