原文地址:http://www.sufeinet.com/thread-6-1-1.html

在线测试工具http://www.sufeinet.com/thread-3690-1-1.html
c# HttpWebRequest与HttpWebResponse 绝技   
如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧。
本文章会对Http请求时的Get和Post方式进行详细的说明,
在请求时的参数怎么发送,怎么带Cookie,怎么设置证书,怎么解决 编码等问题,进行一步一步的解决。
* 如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:
这个类是专门为HTTP的GET和POST请求写的,解决了编码,证书,自动带Cookie等问题。
C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取

1.第一招,根据URL地址获取网页信息
   先来看一下代码
get方法

[C#] 纯文本查看 复制代码U
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
public static string GetUrltoHtml(string Url,string type)
        {
            try
            {
                System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
                // Get the response instance.
                System.Net.WebResponse wResp = wReq.GetResponse();
                System.IO.Stream respStream = wResp.GetResponseStream();
                // Dim reader As StreamReader = New StreamReader(respStream)
                using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(type)))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (System.Exception ex)
            {
                //errorMsg = ex.Message;
            }
            return "";
        }
post方法
[C#] 纯文本查看 复制代码
 
 
 
 
 
 
这招是入门第一式, 特点:

1.最简单最直观的一种,入门课程。

2.适应于明文,无需登录,无需任何验证就可以进入的页面。

3.获取的数据类型为HTML文档。

4.请求方法为Get/Post

2.第二招,根据URL地址获取需要验证证书才能访问的网页信息

先来看一下代码

get方法
[C#] 纯文本查看 复制代码 

 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
 
 
///<summary>
///采用https协议访问网络
///</summary>
///<param name="URL">url地址</param>
///<param name="strPostdata">发送的数据</param>
///<returns></returns>
public string OpenReadWithHttps(string URL, string strPostdata, string strEncoding)
{
    Encoding encoding = Encoding.Default;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
    request.Method = "post";
    request.Accept = "text/html, application/xhtml+xml, */*";
    request.ContentType = "application/x-www-form-urlencoded";
    byte[] buffer = encoding.GetBytes(strPostdata);
    request.ContentLength = buffer.Length;
    request.GetRequestStream().Write(buffer, 0, buffer.Length);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    using( StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
      {
           return reader.ReadToEnd();
      }
}

C# HttpWebRequest 绝技 【转】的更多相关文章

  1. C# HttpWebRequest 绝技 转至 http://www.sufeinet.com/

    转至: 在线测试工具http://www.sufeinet.com/thread-3690-1-1.htmlc# HttpWebRequest与HttpWebResponse 绝技    如果你想做一 ...

  2. [转] C# HttpWebRequest 绝技

    c# HttpWebRequest与HttpWebResponse绝技    阅读原文 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧.本文章会对Http请求时的G ...

  3. C# HttpWebRequest 绝技 根据URL地址获取网页信息

    如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地 ...

  4. C# HttpWebRequest 绝技

    http://www.sufeinet.com/thread-6-1-1.html 万能框架.分布式......

  5. 通过HttpWebRequest调用webService

    调用远端接口,向远端接口写入一个xml文件(loginLog为xml的字符串).关键方法如下: public object InsertAuditLog(string loginLog) { //Ws ...

  6. [C#HttpHelper]类1.4正式版教程与升级报告

       [C#HttpHelper]类1.4正式版教程与升级报告 导读 1.升级报告 2.HttpHelper1.4正式版下载 3.HttpHelper类使用方法, 4.最简单的Post与Get的写法 ...

  7. c# HttpWebRequest与HttpWebResponse 绝技(转载)

    c# HttpWebRequest与HttpWebResponse 绝技    如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧.本文章会对Http请求时的Get和P ...

  8. c# HttpWebRequest与HttpWebResponse

    [转]c# HttpWebRequest与HttpWebResponse 绝技 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧. 本文章会对Http请求时的Get和 ...

  9. 利用HttpWebRequest实现实体对象的上传

    一 简介 HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.它们支持一系列有用的属性.这两个类位 于System.Net命名空间,默认情况下这个类对 ...

随机推荐

  1. hdu 1172 猜数字

    猜数字 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. [AGC010E] Rearranging [拓扑排序+堆]

    题面 传送门 思路 首先,一个显然的结论是:Alice调整过后的序列中任意两个不互质的数的相对顺序无法改变 那么我们可以以这个性质为突破口 我们在两个不互质的权值的点之间连一条边(没错这是个图论题!! ...

  3. 洛谷 P3157 [CQOI2011]动态逆序对 | CDQ分治

    题目:https://www.luogu.org/problemnew/show/3157 题解: 1.对于静态的逆序对可以用树状数组做 2.我们为了方便可以把删除当成增加,可以化动为静 3.找到三维 ...

  4. BZOJ3238 [Ahoi2013]差异 【后缀数组 + 单调栈】

    题目链接 BZOJ3238 题解 简单题 经典后缀数组 + 单调栈套路,求所有后缀\(lcp\) #include<iostream> #include<cstdio> #in ...

  5. 262144 (game)

    262144 (game) 题目描述 Bessie likes downloading games to play on her cell phone, even though she does fi ...

  6. 【CZY选讲·扩展LCS】

    题目描述 给出两个仅有小写字母组成的字符串str1 和str2,试求出两个串的最长公共子序列. 数据范围 |str1| ⩽ 1000; |str2| ⩽ 10^6 题解:    ①直接进行LCS( ...

  7. Vue组件中的单项数据流

    当子组件中的input v-model 父组件的值时不能直接绑定props的值要使用计算属性,向下面的写法,因为props是单项数据流,子组件不能改变父组件的状态,直接绑定会报错. 还可以这样写:但是 ...

  8. 优化web前端性能的几个方法

    1 减少http请求, a. 合并脚本跟样式文件,如可以把多个 CSS 文件合成一个,把多个 JS 文件合成一个. b. CSS Sprites 利用 CSS background 相关元素进行背景图 ...

  9. 【leetcode】500. Keyboard Row

    问题描述: Given a List of words, return the words that can be typed using letters of alphabet on only on ...

  10. 测试 System.SysUtils.TStringHelper

    来自:http://www.cnblogs.com/del/archive/2013/06/14/3135002.html -------------------------------------- ...