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

福州网站建设咨询php网站开发框架有哪些

福州网站建设咨询,php网站开发框架有哪些,新余seo,通用精品课程网站建设的需求分析目录 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.15wanjia.com/news/168139.html

相关文章:

  • 做a免费网站126企业邮箱入口
  • 普陀区网站开发创业做网站失败
  • 想把公司的外部网站替换无锡鑫融建设网站
  • 邵阳市网站建设怎么快速提高网站权重
  • 穿着西裤做的网站4399游戏盒下载官方网站
  • 如何在谷歌上做网站网络广告推广服务
  • wordpress 主机 优点网站seo优化排名
  • 网站 mvc 设计素材下载网
  • 红色大气网络公司企业网站源码_适合广告设计免费公众号开发平台
  • 大型网站建设方案常见问题做视频赚钱的网站有哪些
  • 网站创建想法扬中论坛网官网
  • 网站建设管理经验楚雄企业网站建设公司
  • 网站根目录文件名深圳龙华招聘信息最新招聘
  • 上海网站哪个比较好搜索竞价排名
  • 北京哪里可以申请企业网站域名官网西安制作网站需要多少钱
  • 个人做网站语言石家庄便宜网站制作
  • 学校做网站需要多少钱龙岩网站制作公司
  • 织梦手机电影网站模板网站导航图怎么做的详细步骤
  • 企业网站托管外包怎么做宜兴淘宝网站建设
  • 做网站拉客户有效吗游戏网站开发找什么人可建
  • 会议网站开发wordpress 设置固定链接
  • 工程项目管理软件app生产企业网站如何做seo
  • 为什么后台编辑内容和网站上面显示的内容不一致网站一年的维护费用
  • 做网站能不能放暴露图片海外服务器ip
  • c 网站开发入门视频装修公司经营范围有哪些内容
  • wordpress固定链接中文乐陵seo优化信
  • 做一网站要什么凌源市建设局网站
  • 企业站用什么程序做网站网站产品展示
  • 网站流量统计数据库设计微网站开发教材
  • 农业网站建设模板网站内容管理系统 下载