当前位置: 首页 > news >正文

惠州网站建设如何招生就业网站开发详情

惠州网站建设如何,招生就业网站开发详情,内蒙古呼和浩特邮编,前端页面设计网站目录 1、Demo案例2、请求一个带cookies的get请求3、请求一个带cookies的post请求案例一,案例二的properties的配置 1、Demo案例 public class MyHttpClient {Testpublic void test1() throws IOException {//用来存放我们的结果String result;HttpGet get new Htt…

目录

  • 1、Demo案例
  • 2、请求一个带cookies的get请求
  • 3、请求一个带cookies的post请求
    • 案例一,案例二的properties的配置

image.png

1、Demo案例

public class MyHttpClient {@Testpublic void test1() throws IOException {//用来存放我们的结果String result;HttpGet get = new HttpGet("http://www.baidu.com");//这个是用来执行get方法的HttpClient client = new DefaultHttpClient();HttpResponse response = client.execute(get);result = EntityUtils.toString(response.getEntity(),"utf-8");System.out.println(result);}
}

2、请求一个带cookies的get请求

需要结合mock服务


public class MyCookiesForGet {private String url;private ResourceBundle bundle;//用来存储cookies信息的变量private CookieStore store;@BeforeTestpublic void beforeTest(){bundle = ResourceBundle.getBundle("application", Locale.CHINA);url = bundle.getString("test.url");}@Testpublic void testGetCookies() throws IOException {String result;//       从配置文件中 拼接测试的urlString uri = bundle.getString("getCookies.uri");String testUrl = this.url+uri;//        测试逻辑代码书写HttpGet get = new HttpGet(testUrl);DefaultHttpClient client = new DefaultHttpClient();HttpResponse response = client.execute(get);result = EntityUtils.toString(response.getEntity(),"utf-8");System.out.println(result);//获取cookies信息this.store = client.getCookieStore();List<Cookie> cookieList = store.getCookies();for (Cookie cookie : cookieList){String name = cookie.getName();String value = cookie.getValue();System.out.println("cookie name = " + name+ ";  cookie value = " + value);}}@Test(dependsOnMethods = {"testGetCookies"})public void testGetWithCookies() throws IOException {String uri = bundle.getString("test.get.with.cookies");String testUrl = this.url+uri;HttpGet get = new HttpGet(testUrl);DefaultHttpClient client = new DefaultHttpClient();//设置cookies信息client.setCookieStore(this.store);HttpResponse response = client.execute(get);//获取响应的状态码int statusCode = response.getStatusLine().getStatusCode();System.out.println("statusCode = " + statusCode);if(statusCode == 200){String result = EntityUtils.toString(response.getEntity(),"utf-8");System.out.println(result);}}
}

3、请求一个带cookies的post请求

需要结合mock服务

public class MyCookiesForPost {private String url;  //定义成全局变量private ResourceBundle bundle;//设置成全局变量//用来存储cookies信息的变量private CookieStore store; //定义成全局变量@BeforeTestpublic void beforeTest(){bundle = ResourceBundle.getBundle("application", Locale.CHINA);url = bundle.getString("test.url");}@Testpublic void testGetCookies() throws IOException {String result;
//       从配置文件中 拼接测试的urlString uri = bundle.getString("getCookies.uri");String testUrl = this.url+uri;//        测试逻辑代码书写HttpGet get = new HttpGet(testUrl);DefaultHttpClient client = new DefaultHttpClient();HttpResponse response = client.execute(get);result = EntityUtils.toString(response.getEntity(),"utf-8");System.out.println(result);//获取cookies信息this.store = client.getCookieStore();List<Cookie> cookieList = store.getCookies();for (Cookie cookie : cookieList){String name = cookie.getName();String value = cookie.getValue();System.out.println("cookie name = " + name+ ";  cookie value = " + value);}}@Test(dependsOnMethods = {"testGetCookies"})public void testPostMethod() throws IOException {String uri = bundle.getString("test.post.with.cookies");//拼接最终的测试地址String testUrl = this.url + uri;//声明一个Client对象,用来进行方法的执行DefaultHttpClient client = new DefaultHttpClient();//声明一个方法,这个方法就是post方法HttpPost post = new HttpPost(testUrl);//添加参数JSONObject param = new JSONObject();param.put("name","huhansan");param.put("age","18");//设置请求头信息 设置headerpost.setHeader("content-type","application/json");//将参数信息添加到方法中StringEntity entity = new StringEntity(param.toString(),"utf-8");post.setEntity(entity);//声明一个对象来进行响应结果的存储String result;//设置cookies信息client.setCookieStore(this.store);//执行post方法HttpResponse response = client.execute(post);//获取响应结果result = EntityUtils.toString(response.getEntity(),"utf-8");System.out.println(result);//处理结果,就是判断返回结果是否符合预期//将返回的响应结果字符串转化成为json对象JSONObject resultJson = new JSONObject(result);//获取到结果值String success = (String) resultJson.get("huhansan");String status = (String) resultJson.get("status");//具体的判断返回结果的值Assert.assertEquals("success",success);Assert.assertEquals("1",status);}}

案例一,案例二的properties的配置

test.url=http://localhost:8899
dev.url=http://localhost:8899getCookies.uri=/getCookies
test.get.with.cookies=/get/with/cookies
test.post.with.cookies=/post/with/cookies
login=/login
http://www.yayakq.cn/news/71393/

相关文章:

  • 计算机网站开发是什么专业江门蓬江发布
  • 一般网站建设中的推广费用广州网站建设需要多少钱
  • 如何给网站做流量网站设计配色案列
  • 临城企业做网站做搜狗pc网站
  • 营销型网站建设概述网站设计培训班询
  • 设计网站专业wordpress 阿里云cdn
  • 白家乐网站怎么建站小程序免费推广平台
  • 怎样做网站认证山东网站建设制作公司
  • 企业网站推广策划wordpress大前端主题免费试用
  • 天津网站建设方案策划wordpress列表页怎么加关键词
  • 北京专业建设网站公司哪家好中山网站建设文化机构
  • 遵义市网站建设本科毕设做网站多少钱
  • 建个网站需要多少钱圣宝电动车大架号在哪里c2c电子商务平台有哪些
  • 成都网站排名 生客seo做网站需要掌握的软件
  • wordpress多站点注册页开发应用程序的步骤
  • 网站建设完成汇报百度刷排名seo
  • 吴忠公司做网站增强wordpress编辑器
  • 网站开发的就业前景制作手机app软件要多少钱
  • seo建站是什么龙泉市建设局门户网站
  • python 爬虫 做网站企业网络组建方案
  • 网站建设广告有哪些平台济南软件开发培训机构
  • 网站没被收录什么原因湖南企业app
  • 国外 图片网站百度网站申诉
  • 沈阳网站建设模块维护手机百度官网
  • 注册公司在哪个网站系统网站建设哪家好 需要多少钱
  • 网站开发接入本地天地图深圳专业软件网站建设
  • 深圳网站有哪些网站空间多久续一次费
  • dede 网站打开慢可以做3d电影网站有哪些
  • 个人网站如何获得流量营销型网站网站
  • 虚拟主机销售网站技术支持:淄博网站建设