Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the HttpWebRequest class in C #

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the HttpWebRequest class in C#. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.

HttpWebRequest is an Http request class that inherits from WebRequest.

WebRequest is an abstract class that makes requests for Uniform Resource Identifiers (URIs).

WebRequest has the following derived classes:

System.IO.Packaging.PackWebRequest

System.Net.FileWebRequest

System.Net.FtpWebRequest

System.Net.HttpWebRequest

when used

using System. Net;1, HttpWebRequest instantiation

The following is instantiation method, using visual studio code, will prompt you to simplify the code, the reason is described below

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

HttpWebRequest corresponds to URL, so its connection string must be a valid HTTP string, and the Http protocol type must be added in front of the URL.

may be

http://

https://

Port can be added

http://baidu.com:666

It can also be IP, but also add http header and port.

HttpWebRequest object instantiation, generally not directly new. Instead, use the.Create method to return a WebRequest object.

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

Note the following two methods:

HttpWebRequest.Create

WebRequest.Create

Cretate returns WebRequest objects because Create is a static method.

public static WebRequest Create(string requestUriString); public static WebRequest Create(Uri requestUri); public static WebRequest CreateDefault(Uri requestUri);

So, when creating an HttpWebRequest instance, create it like this:

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

HttpWebRequest supports GET and POST for requests.

setting method

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

The type of request for WebRequest.

http://

https://

ftp://

file://

2, GetResponse Get the result of the request

The HttpWebRequest object uses the.GetResponse() method to get the returned result, and.GetResponse() returns a WebResponse object.

Methods of WebResponse object

Close()

When overridden by a subclass, the response flow is closed.

CreateObjRef(Type)

Create an object that contains all the relevant information needed to generate a proxy for communicating with remote objects.

(Inherited from MarshalByRefObject)Dispose()

Releases unmanaged resources used by WebResponse objects.

Dispose(Boolean)

Releases unmanaged resources used by WebResponse objects and can release managed resources as needed.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)GetHashCode()

As the default hash function.

(Inherited from Object)GetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)GetObjectData(SerializationInfo, StreamingContext)

Populate SerializationInfo with the data needed to serialize the target object.

GetResponseStream()

When overridden in a subclass, returns a stream of data from an Internet resource.

GetType()

Gets the Type of the current instance.

(Inherited from Object)InitializeLifetimeService()

Gets the lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)MemberwiseClone()

Creates a superficial copy of the current Object.

(Inherited from Object)MemberwiseClone(Boolean)

Creates a superficial copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)ToString()

Returns a string representing the current object.

(Inherited from Object)3, get results

Get the data stream using the GetResponseStream() method of the WebSpond object

string Url = "https://www.whuanle.cn:443"; WebRequest wReq = WebRequest.Create(Url); WebResponse wResp = wReq.GetResponse(); System.IO.Stream respStream = wResp.GetResponseStream();4, Get stream information 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(); }

The above is an example of a fetch stream, encoding the output stream in UTF8 and reading it in lines.

Here's another way.

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

ReadToEnd() reads all characters from the stream at once.

About "how to use the HttpWebRequest class in C#" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report