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自动化的更多相关文章

  1. web自动化工具-开篇

    web自动化工具-开篇 最近几年,前端技术风一样的速度迭代更新,各种框架工具雨后春笋般涌现,作为一个平凡的开发者,也只能在洪流中沉沉浮浮,微不足道,以前前端叫做切图仔.美工,如今改了称号叫前端工程师, ...

  2. web自动化工具-liveStyle

    web自动化工具-liveStyle LiveStyle. The first bi-directional real-time edit tool for CSS, LESS and SCSS主要用 ...

  3. web自动化工具-livereload

    web自动化工具-livereload livereload是一个很神奇的工具,主要解放了F5键,监听文件变动,整个页面自动刷新.可搭载gulp等构建工具使用.和liveStyle 针对样式文件相比, ...

  4. web自动化工具-Browsersync

    web自动化工具-Browsersync browser-sync才是神器中的神器,和livereload一样支持监听所有文件.可是和livereload简单粗暴的F5刷新相比,browsersync ...

  5. Selenium Web 自动化 - 项目实战(三)

    Selenium Web 自动化 - 项目实战(三) 2016-08-10 目录 1 关键字驱动概述2 框架更改总览3 框架更改详解  3.1 解析新增页面目录  3.2 解析新增测试用例目录  3. ...

  6. Web自动化框架LazyUI使用手册(2)--先跑起来再说(第一个测试用例-百度搜索)

    作者:cryanimal QQ:164166060 上篇文章中,简要介绍了LazyUI框架,本文便来演示,如何从无到有快速搭建基于lazyUI的工程,并成功运行第一个测试用例. 本文以百度搜索为例,选 ...

  7. Web自动化框架LazyUI使用手册(1)--框架简介

    作者:cryanimal QQ:164166060 web端自动化简介 web端自动化,即通过自动化的方式,对Web页面施行一系列的仿鼠标键盘操作,以达到对Web页面的功能进行自动化测试的目的. 其一 ...

  8. Web自动化框架之五一套完整demo的点点滴滴(excel功能案例参数化+业务功能分层设计+mysql数据存储封装+截图+日志+测试报告+对接缺陷管理系统+自动编译部署环境+自动验证false、error案例)

    标题很大,想说的很多,不知道从那开始~~直接步入正题吧 个人也是由于公司的人员的现状和项目的特殊情况,今年年中后开始折腾web自动化这块:整这个原因很简单,就是想能让自己偷点懒.也让减轻一点同事的苦力 ...

  9. Web自动化框架搭建——前言

    1.web测试功能特性 a.功能逻辑测试(功能测试),这一块所有系统都是一致的,比如数据的添加.删除.修改:功能测试案例设计感兴趣和有时间的话可以另外专题探讨: b.浏览器兼容性测试,更重要的是体验这 ...

随机推荐

  1. CSS3中颜色线性渐变实战

    css3线性渐变可以设置3个参数值:方向.起始颜色.结束颜色.最简单的模式只需要定义起始颜色和结束颜色,起点.终点和方向默认自元素的顶部到底部.下面举例说明: CSS Code复制内容到剪贴板 .te ...

  2. GitHub 里面有大量优秀的第三方框架

    写iOS 程序的时候往往需要很多第三方框架的支持,可以大大减少工作量,讲重点放在软件本身的逻辑实现上. GitHub 里面有大量优秀的第三方框架,而且 License 对商业很友好.一下摘录一下几乎每 ...

  3. 获取屏幕分辨率(C/C++)

    C/C++获取屏幕分辨率的方法 int main(int argc, char* argv[]) { // 需要添加头文件: // #include <Windows.h> system( ...

  4. Delphi初学者应小心的六大陷阱

    Delphi初学者应小心的六大陷阱   作者:子夜编译       初学DelphiI的人,由于各种原因,对DelphiI中的许多概念不能很好的理解,并由此带来了许多的问题,或者是开发出的程序稳性不好 ...

  5. Mapreduce中的字符串编码

    Mapreduce中的字符串编码 $$$ Shuffle的执行过程,需要经过多次比较排序.如果对每一个数据的比较都需要先反序列化,对性能影响极大. RawComparator的作用就不言而喻,能够直接 ...

  6. 内存分配函数malloc、realloc、calloc、_alloca

    1.内存分配函数_alloca.malloc.realloc.calloc: _alloca 函数原型void * __cdecl _alloca(size_t); 头文件:malloc.h _all ...

  7. STM32F0xx_FLASH编程(片内)配置详细过程

    Ⅰ.概述 关于数据的储存,我觉得编程的人基本上都会使用到,只是看你储存在哪里.STM32的芯片内部FLASH都是可以进行编程的,也就是说可以拿来储存数据.但是,很多做一些小应用程序开发的人都没有利用好 ...

  8. [terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046

      SQL_TRACE/10046 事件是 Oracle 提供的用于进行 SQL 跟踪的手段,在日常的数据库问题诊断和解决中是非常常用的方法.但其生成的trace文件需要tkprof工具生成一个可供人 ...

  9. C#实现大数字的运算

    1.添加引用:System.Numerics.dll 2.添加命名空间:using System.Numerics; 3.实例: 3.1判断一个数字是不是质数 static void Main(str ...

  10. hdu 4255 A Famous Grid

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently dis ...