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();
            }
        }

}

  1.  

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. 第 2章 数组和 ArrayLists

    数组是最通用的数据结构,它出现在几乎所有的编程语言里.在 C#语言中使用数组包括创建 System.Array 类型的数组对象,以及创建针对所有数组的抽象的基类型.Array 类提供了一套方法,这些方 ...

  2. Curses library not found. Please install appropriate package

    今天安装mysql-5.5.3-m3的时候,报下面的错误: -- Could NOT find OpenSSL (missing: OPENSSL_LIBRARIES OPENSSL_INCLUDE_ ...

  3. 网络流量监控工具iftop

    #-------------------网络流量监控工具iftop---------------##! /bin/sh #1.首先安装依赖关系包yum install flex byacc libpc ...

  4. 64位操作系统弹出"Failed to load the JNI shared library “E:/2000/Java/JDK6/bin/..jre/bin/client/jvm.dll”

    64位操作系统弹出"Failed to load the JNI shared library /..jre/bin/client/jvm.dll”,最大的可能就是jdk的版本问题.去你的C ...

  5. C#代码利用MongoDB中Group聚合函数查询

    例子: public static void getUserRFM(DateTime beginTime, DateTime endTime)        {            MongoDat ...

  6. Linux rabbitmq的安装和安装amqp的php插件

    RabbitMQ是一个消息代理.它的核心原理非常简单:接收和发送消息.你可以把它想像成一个邮局:你把信件放入邮箱,邮递员就会把信件投递到你的收件人处.在这个比喻中,RabbitMQ是一个邮箱.邮局.邮 ...

  7. 关键字替换排除HTML标签属性字符

    解决办法: 1.打开文件e/class/functions.php 2.找到函数 ReplaceKey($newstext,$classid=0) 3.找到替换代码 if(STR_IREPLACE) ...

  8. Python学习教程(learning Python)--2.3.5 Python返回多个值问题

    本节主要学习Python的函数是如何同时返回多个值的问题. 在学习Python的时候惊奇的发现,Python的函数可以同时返回多个值,很有意思. #define function sum def su ...

  9. windows下找不到strings.h

    头文件用的strings.h,换成string.h就好了.但是以前的Linux系统下用strings.h,strerror都能正常编译,怎么样能正常使用strings.h linux系统下的库问题跟w ...

  10. linux log find 查询

    常用的日志查询命令:find 1.从根目录开始查找所有扩展名为.log的文本文件,并找出包含”ERROR”的行: find / -type f -name "*.log" | xa ...