接口测试网站(HttpClient)

关于httpclient等概念东西上述几位大神的文档中比较多,在这里我们主要简单搭建我们自己所需的最小化框架,并运行起来一、所需环境的安装这里可以参考以前文档中的安装步骤http://blog.csdn.net/yan1234abcd/article/details/49155959我们主要是安装下面两个工具就可以1、JDK的安装和JAVA_HOME的配置注意:这里唯一不一样是,下面项目中的jdk已使用jdk82、Maven安装及配置二、开始编写第一个Test第一个Test,我们写个京东登录接口的功能,下面是具体的实现过程1、在intellij idea创建一个Maven项目FirstAPITest,在pom.xml添加如下依赖,如下图所示在pom.xml里面添加相应的依赖<properties> <!– 指明编译源代码时使用的字符编码,maven编译的时候默认使用的GBK编码, 通过project.build.sourceEncoding属性设置字符编码,告诉maven这个项目使用UTF-8来编译 –> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!–这里可以设置需要运行group–> <groupsTest>execShell</groupsTest> </properties> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.9.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.54</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!–这个引用一直下载不成功,所以只能通过手工下载下来,然后在maven中手动引用 <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> </dependency> –> <!–下面几个引用包是我们手动下载并添加的依赖–> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/json/commons-beanutils-1.7.0.jar</systemPath> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/json/commons-collections-3.1.jar</systemPath> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/json/commons-lang-2.5.jar</systemPath> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/json/commons-logging.jar</systemPath> </dependency> <dependency> <groupId>ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/json/ezmorph-1.0.3.jar</systemPath> </dependency> <dependency> <groupId>json-lib</groupId> <artifactId>json-lib</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/json/json-lib-2.4-jdk15.jar</systemPath> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <threadCount>1</threadCount> <forkCount>1</forkCount> <reuseForks>true</reuseForks> <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine> <forkMode>once</forkMode> <argLine>-Dfile.encoding=UTF-8</argLine> <groups>${groupsTest}</groups> <includes> <include>**/*Test.java</include> </includes> </configuration> </plugin> </plugins> </build>注意:上面json-lib的包没有自动下载成功,所以采用手动下载后,放到项目根目录文件夹json(该目录是手动创建的)中,如下图所示,然后再关联进来,具体见上面配置。之后在编辑器右侧找到Maven Projects插件,在FirstAPITest项目下的LifeCycle中点击 “test”,如下图所示标识1所示。等待项目构建完成后,再点击Maven Projects中的Reimports按钮,如下图所示标识 2所示。该操作是让maven把我们需要用到的Lib自动下载下来。如果之前已经下载过了,则只要单击标识2就可以 如下图所示表示上述标识1的test构建完成2、创建第一个接口测试FirstAPITest项目中,在目录test->java下新建一个类FirstTest.java,如如下图所示2.1 首先使用Fiddler查看京东登录接口写了一会Fiddler的使用方法,但是后来还是觉得网上这些已经写的相当具体了,就直接粘贴链接来更好http://blog.csdn.net/ohmygirl/article/details/17846199下面我们着重看下京东登录接口在Fiddler中的查看。这里我在登录时,用户名和密码都是随便写成test则在Fiddler中捕获到的Http请求可看到,如下图所示的情况上面中可看到红线标注的请求就是京东登录时,我们捕获到的HTTP请求可看到该请求是Post类型,后面是请求对应的URL然后看下右边,这里主要看Inspects这个标签里面的内容,其他标签可看上面链接里面介绍该标签里面分上下两部分,上面是请求相关的内容,下面是响应相关的信息上部分我们可以看下WebForms里面的内容,如下图所示上面红色框中显示的是在京东登录对应的参数。下面红色框中显示的是我们进行登录后,接口返回值,这里我们是看JSON格式的返回值通过上面,我们就可得知京东登录接口的请求类型、请求URL、请求参数。有了这些我们就可以创建一个最简单的登录接口请求2.2使用httpclient使用CSDN登录接口在刚才创建的类FirstTest.java 输入下面内容,这里我们使用Testng作为接口单元测试框架:import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.testng.annotations.Test; import java.util.ArrayList;import java.util.List; public class FirstTest { private HttpClient httpClient = new DefaultHttpClient(); private HttpPost httppost; private HttpResponse response; private HttpEntity entity; private String postResult = null; @Test public void loginJDTest() { String loginURL = “https://passport.jd.com/uc/loginService?uuid=ac394a05-05c0-4667-9940-2848f0c03809&&r=0.8636558873495089&version=2015″; //创建一个httppost请求 httppost = new HttpPost(loginURL); //创建Post请求参数 List<NameValuePair> formparams1 = new ArrayList<NameValuePair>(); formparams1.add(new BasicNameValuePair(“uuid”, “ac394a05-05c0-4667-9940-2848f0c03809″)); formparams1.add(new BasicNameValuePair(“eid”, “Z6RZKBBPUUOORPHGELPNEZE6QHFEIE2IS4EQEBWOKH7VJAPDRIYUCSERJ3DLPNXRINL2ON3JC3IWHIG6L6PQMINSRY”)); formparams1.add(new BasicNameValuePair(“fp”, “4d981fbd1f0cf45fc497eac85348c0f3″)); formparams1.add(new BasicNameValuePair(“_t”, “_nthEAWj”)); formparams1.add(new BasicNameValuePair(“loginType”, “c”)); formparams1.add(new BasicNameValuePair(“loginname”, “test”)); formparams1.add(new BasicNameValuePair(“nloginpwd”, “test”)); formparams1.add(new BasicNameValuePair(“chkRememberMe”, “”)); formparams1.add(new BasicNameValuePair(“authcode”, “”)); formparams1.add(new BasicNameValuePair(“pubKey”, “”)); formparams1.add(new BasicNameValuePair(“sa_token”, “B68C442BE645754F33277E701208059033541C4BBD4BE7A99A94927BD4C8D683E0AC371429CD887688E05A86C51F7C70CE1F231D2A69583750E3923BA39E17118B83A1A1535BAA4F550CDAFD8E0D42172C6687DE87E4A85EA190DE5B35DA60BB878C558875475CC8D7AC4EA721F380156DD8325B976C3C3266E677EACB204C70C0AE07DF75629F5D845602B5968A00D446F66A103CEE76CED0ED85D144B22580F9A9997B4C6606BBA4ACDA2E5675E1753923A7ED53A8CF7426C57380A1EEB3ACC25E396C9D72455D2F8A2EE727961C86C40637A355F83A8A4AC98AB4DB238EA80705100E51EAD01C3E00868DBD94D782ECF89A937893739D7EB48CD4A388D6DA51AF477822A57E7C2708E31DCEB0ADCA88A95D3FB8D20278B263B38D3679DFCD935E41A9612638671D699F59F0E494BF90B522095FF1E38DDB16541245EDE6703833CD15F1AE6D59575C55942B5306C749927362E5D3D7643BA060F53ACFC46F7222511019EBABBCA226F656DC896C7655A90663537C9BBB13657879FA7B99474D4EC9DB2879B409FCE973FA0A9E4E60F4160C3961B55C3401C7CC402917F6A0984544D666FB2C40B6001E1D740C97DAD646E692403DB4A3191F9C638549616672180317153D3EDC2FA55EA740BC4BAE3F5032FD1E3CE7BDA785DDFD5B65AFD0A4CAC07B18AB8B48FFE65E6C25D8E9D9C6B89D4AC495C2FE7937233EC85C60C8C3EA4A9368697EB136B02766908846FE18CF29CA73A07A0D9C3EA0EEB7A75369″)); formparams1.add(new BasicNameValuePair(“seqSid”, “3845068114278942000”)); try { httppost.setEntity(new UrlEncodedFormEntity(formparams1, “UTF-8″)); response = httpClient.execute(httppost); entity = response.getEntity(); // 在这里可以用Jsoup之类的工具对返回结果进行分析,以判断创建是否成功 postResult = EntityUtils.toString(entity, “UTF-8″); System.out.println(“查看登录接口请求返回的结果:” + postResult); } catch (Exception e) { e.printStackTrace(); } httppost.releaseConnection(); }}2.3运行程序直接在编辑器中右键单击,选择“Run “loginJDTest””, 如下图所示运行后,可在日志中查看运行结果,如下图所示则我们完成第一个接口测试啦~~~


本文出自快速备案,转载时请注明出处及相应链接。

本文永久链接: https://www.xiaosb.com/beian/46643/