C# HttpWebRequest 笔记

内容纲要

目录:

 

1,HttpWebRequest 实例化

2,GetResponse 获取请求结果

3,获取结果

4,获取流信息

 

HttpWebRequest 是一个Http 请求类,继承于 WebRequest。

WebRequest 是一个抽象类,能够对统一资源标识符 (URI) 发出请求。

WebRequest 有以下派生类:

 

  • System.IO.Packaging.PackWebRequest
  • System.Net.FileWebRequest
  • System.Net.FtpWebRequest
  • System.Net.HttpWebRequest

使用时 

using System.Net;

1,HttpWebRequest 实例化

以下是实例化方法,使用 visual studio 编写代码时,会提示 可以简化代码,原因下面说

            string url = "http://baidu.com";
            HttpWebRequest httpWeb = (HttpWebRequest)HttpWebRequest.Create(url);

HttpWebRequest 对应的是 URL,所以其连接字符串必须是 有效的 HTTP 字符串,网址前面必须加上 Http 协议类型。

可以是

  • http:// 
  • https:// 

 可以加端口

http://baidu.com:666

也可以是IP,但也要加上 http 头和端口。

HttpWebRequest 对象实例化,一般不直接new。而是使用.Create 方法返回一个 WebRequest 对象。

HttpWebRequest httpWeb = (HttpWebRequest)WebRequest.Create("https://www.whuanle.cn:443");

注意下面两个方法:

HttpWebRequest.Create
WebRequest.Create

Cretate 返回的都是 WebRequest 对象,因为 Create 是一个静态方法

        public static WebRequest Create(string requestUriString);

        public static WebRequest Create(Uri requestUri);

        public static WebRequest CreateDefault(Uri requestUri);

所以,创建HttpWebRequest 实例时,这样创建:

HttpWebRequest httpWeb = (HttpWebRequest)WebRequest.Create("https://www.whuanle.cn:443");

 

HttpWebRequest 支持 GET 和 POST 两种方式进行请求,

设置方法

            HttpWebRequest httpWeb = (HttpWebRequest)WebRequest.Create("https://www.whuanle.cn:443");
            httpWeb.Method = "GET";

 

WebRequest 的请求类型。

  • http://

  • https://

  • ftp://

  • file://

2,GetResponse 获取请求结果

HttpWebRequest 对象 使用 .GetResponse() 方法, 获取返回结果,.GetResponse() 返回的是一个 WebResponse 对象。

WebResponse 对象的方法

Close()

当由子类重写时,将关闭响应流。

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(Inherited from MarshalByRefObject)

Dispose()

释放 WebResponse 对象使用的非托管资源。

Dispose(Boolean)

释放由 WebResponse 对象使用的非托管资源,并可根据需要释放托管资源。

Equals(Object)

确定指定的对象是否等于当前对象。

(Inherited from Object)

GetHashCode()

作为默认哈希函数。

(Inherited from Object)

GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。

(Inherited from MarshalByRefObject)

GetObjectData(SerializationInfo, StreamingContext)     

使用序列化目标对象所需的数据填充 SerializationInfo。

GetResponseStream()

当在子类中重写时,从 Internet 资源返回数据流。

GetType()

获取当前实例的 Type。

(Inherited from Object)

InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。

(Inherited from MarshalByRefObject)

MemberwiseClone()

创建当前 Object 的浅表副本。

(Inherited from Object)

MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(Inherited from MarshalByRefObject)

ToString()

返回表示当前对象的字符串。

(Inherited from Object)

3,获取结果

使用 WebSponse 对象的 GetResponseStream() 方法获取数据流

                string Url = "https://www.whuanle.cn:443";
                WebRequest wReq = WebRequest.Create(Url);
                WebResponse wResp = wReq.GetResponse();
                System.IO.Stream respStream = wResp.GetResponseStream();

 

4,获取流信息

                string Url = "https://www.whuanle.cn:443";
                WebRequest wReq = WebRequest.Create(Url);
                WebResponse wResp = wReq.GetResponse();
                System.IO.Stream respStream = wResp.GetResponseStream();

                using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.UTF8))
                {
                    string a = "";
                    while ((a = reader.ReadLine()) != null)
                    {
                        Console.WriteLine(a);
                    }
                    return reader.ReadToEnd();
                }

上面是一个获取流的例子,以 UTF8 编码输出流,以行读取。

下面是另一个方法

                string Url = "https://www.whuanle.cn:443";
                WebRequest wReq = WebRequest.Create(Url);
                WebResponse wResp = wReq.GetResponse();
                System.IO.Stream respStream = wResp.GetResponseStream();

                using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.UTF8))
                {
                    string str = reader.ReadToEnd();
                    Console.WriteLine(str);
                }

使用 reader.ReadToEnd() 可以一次性从流中读取所有的字符。

 
—————————
推荐一位其他人的文章,里面列出了WebRequest、WebResponse 等对象所有的方法、属性等。
地址:
 
点赞
  1. seroquel cost说道:

    I am also writing to make you know what a impressive encounter my friend's girl gained reading through your web page. She learned plenty of pieces, which include what it's like to possess an amazing helping mood to let most people smoothly know several impossible subject matter. You truly exceeded our own expectations. Thank you for offering these invaluable, safe, explanatory and even fun tips about this topic to Julie.

  2. sinequan no rx说道:

    I precisely desired to say thanks again. I'm not certain the things I could possibly have achieved in the absence of the basics contributed by you about my area of interest. Completely was a challenging circumstance for me, but being able to view a expert approach you dealt with it forced me to cry for contentment. Extremely grateful for the advice and believe you are aware of a powerful job that you're accomplishing educating many people all through a site. I'm certain you've never come across all of us.

  3. I precisely desired to say thanks again. I'm not certain the things I could possibly have accomplished in the absence of the aspects contributed by you about my area. Certainly was a alarming case for me, but being able to see a expert approach you dealt with it forced me to cry for contentment. Extremely grateful for the advice and believe you are aware of a powerful job that you're accomplishing educating many others all through a blog. I'm certain you've never come across all of us.

  4. cbd for pain说道:

    I wanted to put you the very little remark to be able to give many thanks again about the awesome basics you've contributed above. It's generous of people like you giving easily all a few individuals could have advertised as an e-book in making some bucks for their own end, and in particular considering that you could have done it if you wanted. Those thoughts likewise worked to become a great way to recognize that the rest have the same passion the same as my very own to see way more with regards to this problem. I think there are thousands of more pleasurable times up front for those who start reading your website.

发表评论

邮箱地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!