自动化测试--响应请求测试(.net)
Web运行原理简单地说是“浏览器发送一个HTTP Request到Web服务器上,Web服务器处理完后将结果(HTTP Response)返回给浏览器”。
通常测试一个web api是否正确,可以通过自己coding方式模拟向Web服务器发送Http Request(设置好各参数),然后再通过捕获Web服务器返回的Http Response并将其进行解析,验证它是否与预期的结果一致。
.net中提供的Http相关的类
主要是在命名空间System.Net里,提供了以下几种类处理方式:
WebClient
WebRequest WebResponse
HttpWebRequest HttpWebResponse
TcpClient
Socket
(1)使用WebRequest 获取指定URL的内容测试code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net; namespace Testing01
{
class WebRequestTest
{
static void Main(string[] args)
{
string url = @"http://www.baidu.com";
//string html = TestWebClient(url);
//string html = TestWebRequest(url);
string html = TestHttpWebRequest(url);
Console.WriteLine(html);
Console.Read();
} /// <summary>
/// 使用WebClient发出http request
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string TestWebClient(string url)
{
Console.WriteLine("Testing WebClient......");
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url);
string result = "";
using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
{
result = sr.ReadToEnd();
}
return result; } /// <summary>
/// 使用WebClient发出http request
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string TestWebRequest(string url)
{
Console.WriteLine("Testing WebRequest......");
WebRequest wr = WebRequest.Create(url);
wr.Method = "GET";
WebResponse response = wr.GetResponse();
Stream stream = response.GetResponseStream();
string result = "";
using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
{
result = sr.ReadToEnd();
}
return result;
} /// <summary>
/// 使用HttpWebClient发出http request
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string TestHttpWebRequest(string url)
{
Console.WriteLine("Testing HttpWebRequest......");
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.Method = "GET";
HttpWebResponse response = (HttpWebResponse)wr.GetResponse();
Stream stream = response.GetResponseStream();
string result = "";
using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
{
result = sr.ReadToEnd();
}
return result;
}
}
}
(2)获取指定URL(带Cookie)的WebRequest内容测试code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net; namespace Testing01
{
class TestHttpWebRequestWithCookie
{
static void Main(string[] args)
{
string url = "https://passport.xxx.com/account/login";
CookieContainer myCookieContainer = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = myCookieContainer;
string postdata = "bkurl=&LoginName=abc&Password=123456&RememberMe=false";
request.Method = "POST";
request.KeepAlive = true;
byte[] postdata_byte = Encoding.UTF8.GetBytes(postdata);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postdata_byte.Length; Stream stream = request.GetRequestStream();
stream.Write(postdata_byte, , postdata_byte.Length);
stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader srRes = new StreamReader(response.GetResponseStream(),Encoding.UTF8);
string content = srRes.ReadToEnd();
response.Close();
srRes.Close(); string cookieString = request.CookieContainer.GetCookieHeader(new Uri(url));
Console.WriteLine("***********第一次cookie内容:" + cookieString); request = (HttpWebRequest)WebRequest.Create("http://my.xxx.com/xxx/xxx_list.asp");
request.Method = "GET";
request.CookieContainer = myCookieContainer;
request.Headers.Add("Cookie:" + cookieString);
request.KeepAlive = true;
response = (HttpWebResponse)request.GetResponse();
//cookieString = request.CookieContainer.GetCookieHeader(request.RequestUri) + ";" + cookieString;
// Console.WriteLine("***********第二次cookie内容:" + cookieString);
StreamReader srR = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string result = srR.ReadToEnd();
srR.Close();
Console.WriteLine("**************result:" + result.Substring());
Console.Read();
}
}
}
(3)如何跟Https网站交互
我们用浏览器打开HTTPS的网站,如果我们没有安装证书,通常页面会显示 "此网站的安全证书有问题",我们必须再次点"继续浏览此网站(不推荐)"才能查看页面信息.
如下图所示

那么我们的程序,如何忽略HTTPS证书错误呢?
只要在程序中加入下面这段代码,就可以忽略HTTPS证书错误,让我们的程序能和HTTPS网站正确的交互了.
System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
{
return true;
};
(4)采用POST提交Web Request
public static string GetResponse(string url, string method, string data)
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.KeepAlive = true;
req.Method = method.ToUpper();
req.AllowAutoRedirect = true;
req.CookieContainer = CookieContainers;
req.ContentType = "application/x-www-form-urlencoded"; req.UserAgent = IE7;
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Timeout = ; if (method.ToUpper() == "POST" && data != null)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postBytes = encoding.GetBytes(data); ;
req.ContentLength = postBytes.Length;
Stream st = req.GetRequestStream();
st.Write(postBytes, , postBytes.Length);
st.Close();
} System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
{
return true;
}; Encoding myEncoding = Encoding.GetEncoding("UTF-8"); HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream resst = res.GetResponseStream();
StreamReader sr = new StreamReader(resst, myEncoding);
string str = sr.ReadToEnd(); return str;
}
catch (Exception)
{
return string.Empty;
}
}
参考文章地址:http://www.cnblogs.com/TankXiao/archive/2012/02/20/2350421.html
自动化测试--响应请求测试(.net)的更多相关文章
- 第7章使用请求测试-测试API . Rspec: everyday-rspec实操。
测试应用与非人类用户的交互,涵盖外部 API 7.1request test vs feature test 对 RSpec 来说,这种专门针 对 API 的测试最好放在 spec/requests ...
- 使用Windbg找出死锁,解决生产环境中运行的软件不响应请求的问题
前言 本文介绍本人的一次使用Windbg分析dump文件找出死锁的过程,并重点介绍如何确定线程所等待的锁及判断是否出现了死锁. 对于如何安装及设置Windbg请参考:<使用Windbg和SoS扩 ...
- 全--教程API, gem 'rest-client'(用于发简单请求); 请求测试;
安装:rest-client4400✨ gem install rest-client 一个简单的HTTP和REST client for Ruby. 可以用它来发HTTP请求 基本用法: requi ...
- 使用JMeter进行一次简单的带json数据的post请求测试
使用JMeter进行一次简单的带json数据的post请求测试 原文:https://www.cnblogs.com/summer-mm/p/7717812.html 1.启动jmeter:在bin下 ...
- 修改Tomcat响应请求时返回的Server内容
HTTP Server在响应请求时,会返回服务器的Server信息,比如 Tomcat 7 的Header是: 这东西其实会给一些别有用心之人带来一定的提示作用:为安全起见,我们一般会建议去掉或修改这 ...
- 使用C#在CEF中拦截并响应请求
一.前言 忙里偷闲,研究了一下如何在CEF中拦截请求,并作出响应.这个功能对某些需要修改服务器响应的需求来说必不可少,可以直接读取本地文件作为响应内容. C#的CEF封装项目有很多,我使用的是Chro ...
- 零成本实现接口自动化测试 – Java+TestNG 测试Restful service
接口自动化测试 – Java+TestNG 测试 Restful Web Service 关键词:基于Rest的Web服务,接口自动化测试,数据驱动测试,测试Restful Web Service, ...
- 利用python httplib模块 发送Post请求测试web服务是否正常起来!
最近在学习python,恰好老大最近让我搞个基于post请求测试web服务是否正常启用的小监控,上网查了下资料,发现强大的Python恰好能够用上,所以自己现学现卖,顺便锻炼下自己. 由于本人也刚接触 ...
- 如何在Chrome下使用Postman进行rest请求测试
在web和移动端开发时,常常会调用服务器端的restful接口进行数据请求,为了调试,一般会先用工具进行测试,通过测试后才开始在开发中使用.这里介绍一下如何在chrome浏览器利用postman应用进 ...
随机推荐
- Java-idea-运行tomcat 报內存溢出 PermGen space
错误:OutOfMemoryError: PermGen space 非堆溢出(永久保存区域溢出) 在Run/Debug configuration 的你要运行行的tomcat里面的 vm optio ...
- php计算中英文混合或中文字符串的字数
转载来源链接: http://blog.csdn.net/hueise_h/article/details/22920937 php的strlen和mb_strlen用于统计字符个数.中英文混合的字符 ...
- django开发项目的部署nginx
Django 部署(Nginx) 本文主要讲解 nginx + uwsgi socket 的方式来部署 Django,比 Apache mod_wsgi 要复杂一些,但这是目前主流的方法. 1. 运行 ...
- MISC-WHCTF2016-crypto100
题目:李二狗的梦中情人 找不同! 如图,下载得到“nvshen.png” 流程:看到这个被命名为nvshen的文件,感觉文件本身会有东西.用16进制查看器在图片的末尾发现了一串类似URL的ASCII ...
- (转)JavaScriptSerializer,DataContractJsonSerializer解析JSON字符串功能小记
JsonAbout: using System;using System.Collections.Generic;using System.Linq;using System.Text;using S ...
- 《FontForge常见问题FAQ》字王翻译版
<FontForge常见问题FAQ> 字王翻译版 原文: http://fontforge.github.io/en-US/faq/ 翻译: 字王·中国 blog: http://bl ...
- Cup fungus in Corvobado Nation Park,Costa Rica
- linux下 ip指令
目录 Network ip command Command :ip 简介 内容 Network ip command Command :ip 简介 ip 是個指令喔!並不是那個 TCP/IP 的 IP ...
- UVa 1635 无关的元素(唯一分解定理+二项式定理)
https://vjudge.net/problem/UVA-1635 题意: 给定n个数a1,a2,...an,依次求出相邻两数之和,将得到一个新数列.重复上述操作,最后结果将变成一个数.问这个数除 ...
- 一款表达谱数据分析的神器--CCLE--转载
现在做生物和医学的,很多都可能会和各种组学数据打交道.其中表达谱数据总是最常用的,也是比较好测的.即使在工作中不去测序,也可以利用已有的数据库去做一些数据挖掘,找一找不同表型(比如癌症)对应的mark ...