原文地址: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. LeetCode -- Sum Root to Leaf NNumbers

    Related Links: Path Sum: http://www.cnblogs.com/little-YTMM/p/4529982.html Path Sum II: http://www.c ...

  2. BZOJ5301 [Cqoi2018]异或序列 【莫队】

    题目链接 BZOJ5301 题解 莫队水题 BZOJ400AC纪念 #include<algorithm> #include<iostream> #include<cst ...

  3. rem布局和hotcss原理分析

    rem布局的开源方案hotcss, 其原理个人理解如下: 手机px = (手机页面宽度/设计稿宽度) * 设计稿px 手机rem = 手机px / fontSize = (手机页面宽度/设计稿宽度) ...

  4. Disable or enable the IPv6 protocol in Red Hat Enterprise Linux

    Resolution Red Hat Enterprise Linux 4, 5 and 6 enable Internet Protocol Version 6 (IPv6) by default. ...

  5. php命名空间与可变函数

    命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误.这种情况下只要避免命名重复就可以解决 对于命名空间,官方文档已经说得很详细[查看],我在 ...

  6. react dva routerRedux 备忘

    首先你需要import { Link, routerRedux } from 'dva/router'; 在方法里跳转用 function applyJobHandler(){ dispatch(ro ...

  7. 过河(DP)

    原题传送门 这道题要用到压缩的思想(原来DP还能这么用...) 其实很简单,假如我们要到某一个位置w 如果我们原位置为Q 很显然,如果(W-Q>=s*t)那么我们一定能到达W 换言之,就是如果我 ...

  8. rootkit 内核函数hook

    转自:https://0x90syntax.wordpress.com/2016/02/21/suterusu-rootkitx86%e4%b8%8earm%e7%9a%84%e5%86%85%e8% ...

  9. OPEN SUSE LINUX

    1. 把中文界面变成英文界面 yast2->system->language: 主要语言: 美式英语US 2. 使用root用户默认登录 Ubuntu使用root登录        Ubu ...

  10. How do I list all fields of an object in Objective-C?

    http://stackoverflow.com/questions/1213901/how-do-i-list-all-fields-of-an-object-in-objective-c As m ...