HttpClient _httpClient;
HttpClient _businessHttpClient; private async Task<string> GetAccessToken()
{
_businessHttpClient = new HttpClient();
_businessHttpClient.BaseAddress = new Uri("http://localhost:8998/Meet.WebApi/");
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri("http://localhost:8998/HRManage.WebApi/api/UserLogin?username=admin&password=111111");
var parameters = new Dictionary<string, string>();
parameters.Add("username", "admin");
parameters.Add("password", "");
var response = await _httpClient.PostAsync(_httpClient.BaseAddress, new FormUrlEncodedContent(parameters));
var responseValue = await response.Content.ReadAsStringAsync(); if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var data = JsonConvert.DeserializeObject<ResultEntity<UserLoginResultDTO>>(responseValue);
return data.Data.AccessToken;
}
else
{
return string.Empty;
}
} [TestMethod]
public void GetMeetingById()
{
string tokenstring = GetAccessToken().Result; _businessHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenstring);
var response = _businessHttpClient.GetAsync("Meet/GetMeetingById?Id=5C0062FF-542A-4C6E-BE28-5998EC3CEDE4").Result;
var responseValue = response.Content.ReadAsStringAsync().Result;
_businessHttpClient.Dispose();
var data = JsonConvert.DeserializeObject<ResultEntity<MeetingOutDto>>(responseValue);
Assert.AreEqual(true, JObject.Parse(responseValue)["IsSuccess"].Value<bool>(), JObject.Parse(responseValue)["Msg"].Value<string>());
}

Post方式:

        HttpClient _httpClient;
//模拟登陆
public async Task<bool> Login()
{
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri("http://localhost:8088");//14199 //
LoginInput log = new LoginInput() { mobile = "", pwd = "" }; HttpContent httpcontent = new StringContent(JsonConvert.SerializeObject(log));
httpcontent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = await _httpClient.PostAsync("hcApi/Login/login", httpcontent);
var responseValue = await response.Content.ReadAsStringAsync(); if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return JObject.Parse(responseValue)["isOK"].Value<bool>();
}
else
{
return false;
}
}
/// <summary>
/// 根据条件获取卫生院信息
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task GetHealthCenterInfo()
{
await Login(); var response = await _httpClient.GetAsync("hcApi/SampleEntry/GetHealthCenterInfo?hcID=57&&keyWord=&&pageIndex=1&&pageSize=10");
var responseValue = await response.Content.ReadAsStringAsync();
_httpClient.Dispose();
Assert.AreEqual(, JObject.Parse(responseValue)["errorCode"].Value<int>(), JObject.Parse(responseValue)["msg"].Value<string>());
}

单元测试,模拟用户Get登陆,并携带登录后的token访问接口的更多相关文章

  1. 登录后保存token值到cookie中

    1.引入相应JS <script src="web/js/jquery-1.9.1.min.js"></script> <script src=&qu ...

  2. Win 2008 r2 远程桌面多用户登陆,一用户多登陆配置

    Windows 2008 R2远程桌面,设置最大连接数,一个登录后另一个就被踢掉等问题 Windows 2008 R2配置如图: 1.打开远程桌面回话主机配置 2.右键RDP-Tcp,属性,可设置最大 ...

  3. 利用cookies获取登录后的网页

    众所周知,HTTP连接是无状态的,那么问题来了,怎么记录用户的登录信息呢?通常的做法是用户第一次发送HTTP请求时,在HTTP Server端生成一个SessionID,SessionID会对应每个会 ...

  4. c# winform实现网页上用户自动登陆,模拟网站登录

    using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...

  5. 运用String类实现一个模拟用户登录程序

    package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...

  6. python3.0 模拟用户登录,三次错误锁定

    # -*- coding:utf-8 -*- #需求模拟用户登录,超过三次错误锁定不允许登陆     count = 0   #realname passwd Real_Username = &quo ...

  7. scrapy模拟用户登录

    scrapy框架编写模拟用户登录的三种方式: 方式一:携带cookie登录,携带cookie一般请求的url为登录后的页面,获取cookie信息应在登录后的页面获取,cookie参数应转成字典形式 # ...

  8. python小练习--模拟用户登录,(3次重试机会,登录成功展示登录账号密码)

    知识点使用:1.格式化输出的两种方法---% .formate 2.while循环的使用,及跳出循环的两种方法---break(跳出循环体).continue(结束本次循环,继续下次循环) 3.if条 ...

  9. Golang模拟用户登陆,突破教务系统

    目录 一.Golang模拟用户登陆,突破教务系统 1.1 请求登陆页面 1.2 抓包分析登陆请求 1.3 golang使用js引擎合成salt 1.4 模拟表单提交,完成登陆 1.5 进入成绩查询页, ...

随机推荐

  1. 12 二叉树-链式存储-二叉排序树(BST)

    呜呜 写这个东西花了我2天 居然花了两天!!我还要写AVL呢啊啊啊啊啊啊啊!!!!!! 等下还要跑去上自习 大早上起来脸都没洗现在先赶紧发博客 昨晚写出来了独自在其他人都睡着了的宿舍狂喜乱舞.. 迷之 ...

  2. XAML属性赋值转换之谜(WPF XAML语法解密)

    XAML与XML类似,就是XML延伸过来的.为了更好的表达一些功能,WPF对XML做了扩展,有些功能是WPF在后台悄悄的替你做了.有时候,虽然实现了某个功能,但是对实现原理还是很茫然.今天就讲讲XAM ...

  3. freemark null处理

    以下引用官方描述: 引用The FreeMarker template language doesn't know the Java language null at all. It doesn't ...

  4. map集合的见解、排序

    map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等 HashMap:我们最常用的Map,它根据key的HashCode 值 ...

  5. Java中的八大基本数据类型

    Java中的基本类型主要包括整数.浮点数.字符和布尔类型. 整数: 整数类型主要包括byte.short.int.long四种类型,所占大小分别为1byte.2byte.4byte和8byte: 浮点 ...

  6. CustomSqlSessionFactoryBean

    import java.io.File; import java.net.JarURLConnection; import java.net.URL; import java.util.ArrayLi ...

  7. 简单聊聊SOA和微服务

    转自:https://juejin.im/post/592f87feb123db0064e5ef7c  (2017-06) 简单聊聊SOA和微服务 架构设计中的朴素主义 前两天和一个朋友聊天,他向我咨 ...

  8. Log4j和Log4j2的区别

    Log4j是Apache的一个开源项目,我们不去考究它的起源时间,但是据我了解,log4j 1已经不再更新了. 下面我就以列举的方式来浅谈log4j和log4j 2的不同之处. 一.配置文件类型 lo ...

  9. PowerBuilder编程新思维5:包装(界面美化与WebUI+React)

    PowerBuilder编程新思维5:包装(界面美化与WebUI+React) 前一节,分析了三种界面美化方案,都是控件级的美化.今天再来分析一下窗口级的美化.上一次讲的DirectUI,大家反响一般 ...

  10. input输入框file类型第二次不触发onchange事件的解决办法,简单有效

    在网上看了很多办法,现在将网上大部分说法总结如下: 网上说法: 原因:选择一次后onchange事件没有绑定到input标签上:    解决办法:拷贝一份input标签的副本,每次选择后对原input ...