httphelp web自动化
public class HttpHelper
{
public static CookieContainer CookieContainers = new CookieContainer();
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <param name="method">"POST" or "GET"</param>
/// <param name="data">when the method is "POST", the data will send to web server, if the method is "GET", the data should be string.empty</param>
/// <returns></returns>
public static string GetPostResponse(string url, string data,string referer)
{
StreamReader stReader = null;
HttpWebRequest request=null;
HttpWebResponse response=null;
try
{
request = (HttpWebRequest)HttpWebRequest.Create(url);
string dataStr = data;
byte[] postData = Encoding.GetEncoding("GBK").GetBytes(data);
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version11;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.861.0 Safari/535.2";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
request.Referer = referer;
request.CookieContainer = CookieContainers;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Headers.Add("Cache-Control", "max-age=0");
request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");
request.Headers.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
Stream st = request.GetRequestStream();//将st名为写入数据流
st.Write(postData, 0, postData.Length);//写入st数据流
st.Close();
request.AllowAutoRedirect = false;
response = (HttpWebResponse)request.GetResponse();
st = response.GetResponseStream();
CookieContainers.Add(response.Cookies);
if (response.ContentEncoding.ToLower().Contains("gzip"))
{
st = new GZipStream(st, CompressionMode.Decompress, true);
}
string htmlText;
stReader = new StreamReader(st, Encoding.GetEncoding("GBK"));
htmlText = stReader.ReadToEnd();
return htmlText;
}
catch (Exception ex)
{
throw ex;
}
finally
{
stReader.Close();
response.Close();
}
}
public static string GetResponse(string url, string referer)
{
HttpWebRequest request=null;
HttpWebResponse response=null;
StreamReader stReader =null;
try
{
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "GET";
request.ProtocolVersion = HttpVersion.Version11;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.861.0 Safari/535.2";
request.ContentType = "application/x-www-form-urlencoded";
if (referer != "")
{
request.Referer = referer;
}
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.CookieContainer = CookieContainers;
request.Headers.Add("Cache-Control", "max-age=0");
request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");
request.Headers.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
request.AllowAutoRedirect = false;
response = (HttpWebResponse)request.GetResponse();
Stream st = response.GetResponseStream();
CookieContainers.Add(response.Cookies);
if (response.ContentEncoding.ToLower().Contains("gzip"))
{
st = new GZipStream(st, CompressionMode.Decompress, true);
}
string htmlText;
stReader = new StreamReader(st, Encoding.GetEncoding("GBK"));
htmlText = stReader.ReadToEnd();
return htmlText;
}
catch (Exception ex)
{
throw ex;
}
finally
{
stReader.Close();
response.Close();
}
}
}
httphelp web自动化的更多相关文章
- web自动化工具-开篇
web自动化工具-开篇 最近几年,前端技术风一样的速度迭代更新,各种框架工具雨后春笋般涌现,作为一个平凡的开发者,也只能在洪流中沉沉浮浮,微不足道,以前前端叫做切图仔.美工,如今改了称号叫前端工程师, ...
- web自动化工具-liveStyle
web自动化工具-liveStyle LiveStyle. The first bi-directional real-time edit tool for CSS, LESS and SCSS主要用 ...
- web自动化工具-livereload
web自动化工具-livereload livereload是一个很神奇的工具,主要解放了F5键,监听文件变动,整个页面自动刷新.可搭载gulp等构建工具使用.和liveStyle 针对样式文件相比, ...
- web自动化工具-Browsersync
web自动化工具-Browsersync browser-sync才是神器中的神器,和livereload一样支持监听所有文件.可是和livereload简单粗暴的F5刷新相比,browsersync ...
- Selenium Web 自动化 - 项目实战(三)
Selenium Web 自动化 - 项目实战(三) 2016-08-10 目录 1 关键字驱动概述2 框架更改总览3 框架更改详解 3.1 解析新增页面目录 3.2 解析新增测试用例目录 3. ...
- Web自动化框架LazyUI使用手册(2)--先跑起来再说(第一个测试用例-百度搜索)
作者:cryanimal QQ:164166060 上篇文章中,简要介绍了LazyUI框架,本文便来演示,如何从无到有快速搭建基于lazyUI的工程,并成功运行第一个测试用例. 本文以百度搜索为例,选 ...
- Web自动化框架LazyUI使用手册(1)--框架简介
作者:cryanimal QQ:164166060 web端自动化简介 web端自动化,即通过自动化的方式,对Web页面施行一系列的仿鼠标键盘操作,以达到对Web页面的功能进行自动化测试的目的. 其一 ...
- Web自动化框架之五一套完整demo的点点滴滴(excel功能案例参数化+业务功能分层设计+mysql数据存储封装+截图+日志+测试报告+对接缺陷管理系统+自动编译部署环境+自动验证false、error案例)
标题很大,想说的很多,不知道从那开始~~直接步入正题吧 个人也是由于公司的人员的现状和项目的特殊情况,今年年中后开始折腾web自动化这块:整这个原因很简单,就是想能让自己偷点懒.也让减轻一点同事的苦力 ...
- Web自动化框架搭建——前言
1.web测试功能特性 a.功能逻辑测试(功能测试),这一块所有系统都是一致的,比如数据的添加.删除.修改:功能测试案例设计感兴趣和有时间的话可以另外专题探讨: b.浏览器兼容性测试,更重要的是体验这 ...
随机推荐
- Django搭建及源码分析(一)
一.关于Django以下两个站点,在使用方面有详细说明. http://www.nowamagic.net/academy/part/13/286 http://www.w3cschool.cc/dj ...
- DPDK内存管理-----(三)rte_malloc内存管理
rte_malloc()为程序运行过程中分配内存,模拟从堆中动态分配内存空间. void * rte_malloc(const char *type, size_t size, unsigned al ...
- LoadCursor 函数
从可执行文件中载入指定的光标资源,加载到指定的应用实例中 ? 1 2 3 4 5 HCURSOR WINAPI LoadCursor( _In_opt_ HINSTANCE hInstance, ...
- webserver 发布问题
1:web.config <system.web> <compilation debug="true" targetFramework="4. ...
- ref和out的区别?
ref 和out的区别在面试中会常问到: 首先:两者都是按地址传递的,使用后都将改变原来参数的数值. 其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传 ...
- 取消双向绑定、输出html代码
1.取消双向绑定,在绑定的值前加*号. 如: <div id="app"> <p>{{*message}}</p> </div> 2 ...
- Mysql常用数据类型详细说明及实例说明(学习笔记一)
1.Mysql 在windows下 Net start mysql[启动] Net stop mysql[停止] Quit[退出mysql命令行] \c[取消输入的命令] Select version ...
- WindowManager和WindowManager.LayoutParams的使用以及实现悬浮窗口的方法
写Android程序的时候一般用WindowManager就是去获得屏幕的宽和高,来布局一些小的东西.基本上没有怎么看他的其他的接口. 这两天想写一个简单的类似于Toast的东西,自定义布局,突然发现 ...
- Yii框架中使用PHPExcel导出Excel文件
最近在研究PHP的Yii框架,很喜欢,碰到导出Excel的问题,研究了一下,就有了下面的方法: 1.首先在config\main.php中添加对PHPExcel的引用,我的方式是这样: 1 2 3 4 ...
- phpcms v9 中get的mysql查询表某字段最大值数据,表某字段不重复数据
直切正题 1.表tb中字段num最大的数据 {pc:get $sql="select * from tb where num=(select MAX(num) from tb)"} ...