C#实现HTTP访问类HttpHelper的示例详解
1. 什么是HttpHelper类
HttpHelper类是一个用于实现HTTP访问的工具类,通过该类可以实现Http的GET、POST、PUT、DELETE等请求。
2. 如何使用HttpHelper类
使用HttpHelper类需要先引入命名空间:
using System.Net;
接着,可以通过以下代码创建一个 HttpHelper 对象:
public class HttpHelper
{
public HttpHelper()
{
ServicePointManager.DefaultConnectionLimit = 512;
ServicePointManager.Expect100Continue = false;
ServicePointManager.MaxServicePointIdleTime = 2000;
ServicePointManager.UseNagleAlgorithm = false;
}
}
其中,通过设置 ServicePointManager,可以优化连接池的连接数、请求超时时间、Nagle算法等。接下来我们来讲解 HttpHelper 类里面的方法。
3. HttpHelper 方法详解
3.1 GET 请求
使用 GET 请求需要传入一个 URL 参数,同时可以指定 Header 信息和超时时间。以下是示例代码:
public string Get(string url, string[] headers = null, int timeout = 30000)
{
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "GET";
request.Timeout = timeout;
if (headers != null)
{
foreach (string header in headers)
{
request.Headers.Add(header);
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
if (stream != null)
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
}
catch (Exception ex)
{
// 异常处理
}
return string.Empty;
}
3.2 POST 请求
使用 POST 请求需要传入一个 URL 和一个字典形式的 data 参数,同时可以指定 Header 信息和超时时间。以下是示例代码:
public string Post(string url, Dictionary<string, string> data, string[] headers = null, int timeout = 30000)
{
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.Timeout = timeout;
request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
if (headers != null)
{
foreach (string header in headers)
{
request.Headers.Add(header);
}
}
StringBuilder builder = new StringBuilder();
foreach (var item in data)
{
builder.AppendFormat("{0}={1}&", item.Key, item.Value);
}
builder.Remove(builder.Length - 1, 1);
byte[] bytes = Encoding.UTF8.GetBytes(builder.ToString());
request.ContentLength = bytes.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
if (stream != null)
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
}
catch (Exception ex)
{
// 异常处理
}
return string.Empty;
}
4. 示例
4.1 示例1
以下是一个使用 GET 请求的示例:
string url = "https://www.example.com";
HttpHelper helper = new HttpHelper();
string result = helper.Get(url);
Console.WriteLine(result);
4.2 示例2
以下是一个使用 POST 请求的示例:
string url = "https://www.example.com/post";
Dictionary<string, string> data = new Dictionary<string, string>();
string[] headers = new string[] { "Accept-Encoding:gzip, deflate, br" };
data.Add("param1", "value1");
data.Add("param2", "value2");
HttpHelper helper = new HttpHelper();
string result = helper.Post(url, data, headers);
Console.WriteLine(result);
5. 总结
本文详细讲解了 C# 实现 HTTP 访问类 HttpHelper 的示例详解,包括 HttpHelper 类的创建、GET 和 POST 请求的实现以及提供了两条示例进行参考。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#实现HTTP访问类HttpHelper的示例详解 - Python技术站