C# 发送HTTP请求(可加入Cookies)
2023-09-11 14:21:56 时间
C# 发送HTTP请求(可加入Cookies)
MNTM在2018年11月16日发布
在C#中,发送一个HTTP请求还是相当容易的,而且与Java的方法特别相似,我们并不需要使用socket来实现这个功能,用C#的HttpWebRequest类就能实现了。最后写出的函数是可以请求一个指定URL的,并且可以带上Cookies。
0x01 HttpWebRequest类的使用方法
构造HttpWebRequest对象
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
Url变量为要访问的Url
设置属性(设置请求头)
request.Method = "POST"; //GET或POST请求
request.ContentType = "application/x-www-form-urlencoded"; //Content-Type头
request.ContentLength = postDataStr.Length; //设置Content-Length头
常用属性
Method 请求方式
ContentType Content-Type头
ContentLength Content-Length头,正文的长度
Accept Accept头,接受的类型
KeepAlive 是否保持HTTP连接
Headers 请求头的集合,可以添加/修改/删除某个请求头
UserAgent User-Agent头,用于设置浏览器名
Proxy 获取或设置代理服务器的信息
常用函数方法
获取请求流(一般用于添加表单数据等)
request.GetRequestStream();
获取响应对象
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
添加请求头
request.Headers.Add("xxx", val); //添加请求头xxx,的值为val
0x02 HttpWebResponse类的使用方法
常用属性
Headers 请求头的集合,可以添加/修改/删除某个请求头
ContentEncoding 响应包的编码格式
ContentLength Content-Length头,正文的长度
Cookies 回传的Cookies集合
StatusCode HTTP响应状态码
常用函数方法
获取响应流(一般用于获取回传数据包的正文内容)
response.GetResponseStream();
获取响应头
response.GetResponseHeader("xxx");
0x03 封装一个HttpRequest类
代码如下
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text;
/// <summary>
///HttpRequest 的摘要说明
/// </summary>
public class HttpRequest{
public static string Post(string Url, string postDataStr,string cookies){
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
if(cookies!=null)
request.Headers.Add("Cookie", cookies);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataStr.Length;
// 参数
StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
writer.Write(postDataStr);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
return retString;
}
public static string Get(string Url,string cookies){
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "GET";
if(cookies!=null)
request.Headers.Add("Cookie", cookies);
request.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
return retString;
}
public HttpRequest()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
}
相关文章
- HTTP could not register URL http://+:8000/testservice/. Your...
- 教程-Delphi调用C# WEBSERVICE(二)
- (C#)Application.Exit()、Environment.Exit(0)区别
- 12篇学通C#网络编程——第二篇 HTTP应用编程(上)
- 《C#高级编程》学习笔记----c#内存管理--栈VS堆
- C#的一维数组和二维数组定义方式:
- Scala Http 请求调用 - scalaj-http
- [Http] Understand what an HTTP Request is
- 解决docker push镜像到私有仓库时的报错【http: server gave HTTP response to HTTPS client】
- C# 上传下载ftp(支持断点续传)
- C# index of 用法(转载)
- 使用 http-proxy 代理 SAP UI5 应用发起的 HTTP 请求
- C# 字符串中多个连续空格转为一个空格
- Atitit.http httpclient实践java c# .net php attilax总结
- Atitit.http代理的实现 代码java php c# python
- Atitit.http代理的实现 代码java php c# python
- Atitit.http代理的实现 代码java php c# python
- 使用 http-proxy 代理 SAP UI5 应用发起的 HTTP 请求
- C#基础——基本数据类型、常量及变量
- C#请求访问HTTP+JSON数据的解析
- 思科恶意加密TLS流检测论文记录——由于样本不均衡,其实做得并不好,神马99.9的准确率都是浮云啊,之所以思科使用DNS和http一个重要假设是DGA和HTTP C&C(正常http会有图片等)。一开始思科使用的逻辑回归,后面17年文章是随机森林。
- C# .NET 中的缓存实现详情
- C#用SOCKET发送HTTP请求小例
- C# winform窗体程序-菜单动态合并(MDI窗体)