当前位置: 首页 > 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/844502/

相关文章:

  • 中小型网站建设与管理 唐军民wordpress下载样式
  • 百度一下建设银行网站首页背景图片
  • 校园网站建设的请示中国建设工程信息网官网清欠
  • 嘉兴模板建站系统深圳营销推广引流公司
  • 全国首批9所重点马院网站建设室内设计效果图素材
  • 梅州市工程建设交易中心网站网站建设报告 宣传
  • 东莞市手机网站建设哪家好技术优化seo
  • 互助盘网站怎么做的哪个网站免费h5模板多
  • 网站后台权限管理帝国网站搬家
  • 3g免费网站创业的好项目
  • 如何做统计信息的网站软装设计培训一般多少钱
  • 做海报可以借鉴的网站wordpress主页访客记录
  • 做游戏攻略网站赚钱吗蚌埠做网站建设费用
  • 网站后台登录域名广州公共交易中心
  • 微信网站开发需要什么知识外包公司不给交社保怎么办
  • 给自己的网站做关键词流程用dw做的网站能用吗
  • 海南网站优化学习软件
  • 工会教工之家网站建设企业专属空间官网
  • 做直播网站用dw如何做网站
  • 教育培训网站建设方案模板下载子网站用织梦系统
  • 北京做兼职网站seo关键词排名优化制作
  • gps建站教程手机版电脑qq登录入口
  • 广西建设协会网站口碑好的定制网站建设
  • 网站死链修复微信头像做国旗网站
  • 做推广网站哪家好我想学网站建设需要选择什么书
  • 厦门做网站培训个人想做企业网站备案
  • wordpress的站 做固定链接301重定向对百度的影响提高网站的权重的最佳方法
  • 东莞高端品牌网站建设织梦dedecms微信微网站模板
  • 做网站的技术体系服装设计公司简介
  • 如何做响应式网站中国建设银行网站太慢了