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.浏览器兼容性测试,更重要的是体验这 ...
随机推荐
- javascript 数组对象与嵌套循环写法
'use strict' var info=[{"name":"最近想跳河","interst":["历史"," ...
- CentOS 6 lnmp环境脚本
实验环境:CentOS 6.3 32位 首先我们先去下载nginx的第三方yum源 mkdir /shell cd /shell wget http://www.atomicorp.com/insta ...
- vs C#数据库导入EXCLE
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 关于Ajax跨域
本人因工作需求,编写了一个测试页面,在页面填写完信息之后去向一个站点请求数据,然后返回结果!一开始是直接用Ajax在脚本中去访问,没有大碍(因为目标地址是本机上的一个网站),但是当站点去外部的网站时, ...
- SQL中如何检查死锁
SQL中如何检查死锁 编写人:CC阿爸 2014-6-15 在日常SQL数据库的操作中,SQL偶尔会出现表被死锁的问题.比如: 在执行事务时,突然中止事务.系统肯定会锁表. 大批量数据操作时,由于网络 ...
- seeting菜单界面形成--优化
本文是上一篇文章的优化版: 上文链接地址:http://www.cnblogs.com/zzw1994/p/5016864.html 上文中有很多方法都是过时,并且效率不是很高,主要对JAVA代码进行 ...
- PowerShell 方式部署Sharepoint Solution
覆盖 Uninstall-SPSolution –Identity Caesarstone.GlobalSite.WebSite.wsp –WebApplication http://myserver ...
- Oracle存储过程学习备忘
之前的项目使用存储过程很少,但在实际的项目中,存储过程的使用是必不可少的. 存储过程是一组为了完成特定功能的SQL 语句 集,经编译后存储在数据库中:存储过程创建后,一次编译在程序中可以多次调用,对安 ...
- Ztack学习笔记(6)-广播组播点播
Zigbee网络中进行数据通信主要有三种类型:单播.组播.广播.那这三种方式如何设置呢,在哪里设置呢, 一. 广播 当应用程序需要将数据包发送给网络的每一个设备时,使用这种模式.广播的短地址有三种 0 ...
- <?php>慢慢写一些php的cookie问题<?>
写网站是个爬坑的过程,在你设计完功能之后,就会发现:卧槽,这个怎么实现?你妹,这个能实现么? 进了公司分工明确还好说(= =学长们都这么说),在学校自己没事写一些项目的话只能自己爬坑了. 蹬蹬瞪蹬,登 ...