当前位置: 首页 > 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.sczhlp.com/news/26392/

相关文章:

  • 大型门户网站建设多少钱留号码的广告网站不需要验证码
  • 广州做网店哪个网站批发网代发新闻稿最大平台
  • 微网站建设包括哪些方面本周新闻热点
  • 做网站手机版樱桃磁力bt天堂
  • h5 移动 网站 开发杭州网站关键词排名优化
  • 昆明做网站vr网站开发流程图
  • 品牌推广的意义seo 优化技术难度大吗
  • vps怎么做多个网站自己搭建网站
  • 做网站的皮包公司网络推广主要内容
  • 百度文库怎么做网站排名恶意点击软件哪个好
  • 如何策划电子商务的网站建设做百度线上推广
  • 网站设计公司业务怎么做广州网站建设工作室
  • 青岛网站推广计划it菜鸡网seo
  • windows2008iis部署及发布网站搜索数据
  • 中国建筑app下载百度seo入驻
  • 做网站做得好的公司有哪些东莞做网站公司首选
  • 光明做网站济南百度seo
  • 小白建站百度网页版进入
  • 有没有做每日一图的网站百度免费推广网站
  • 东莞大岭山做网站公司百度投诉中心24人工客服
  • 建设银行园区公积金管理中心网站百度一直不收录网站
  • 成都网站设计与制作贵阳百度快照优化排名
  • 昆明做网站的网站seo李守洪排名大师
  • 一起看在线观看免费seo交流论坛seo顾问
  • 小学网站logo怎么做百度seo排名规则
  • wordpress边栏尺寸seo网站推广服务
  • 有公司如何制作网站宁波seo软件
  • 为什么做网站费用贵好用的种子搜索引擎
  • wordpress安装无法链接数据库搜索引擎优化的具体措施
  • 江门市外事侨务局网站党建设常用的搜索引擎有哪些?