-
Notifications
You must be signed in to change notification settings - Fork 16
XiaoFeng.Http.HttpHelper 网络请求库
Jacky edited this page Mar 8, 2023
·
1 revision
HttpHelper 是Http模拟请求库。提供了三种内核,HttpClient,HttpWebRequest,HttpSocket 默认用的是HttpClient内核
- GET 请求
var result = await XiaoFeng.Http.HttpHelper.GetHtmlAsync(new XiaoFeng.Http.HttpRequest
{
Method = HttpMethod.Get,//不设置默认为Get请求
HttpCore = HttpCore.HttpClient,//不设置默认为HttpClient内核
Address = "http://www.fayelf.com"
});
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
/*请求成功*/
//响应内容
var value = result.Html;
//响应内容字节集
var bytes = result.Data;
}
else
{
/*请求失败*/
}
- POST 表单请求
var result = await XiaoFeng.Http.HttpHelper.GetHtmlAsync(new XiaoFeng.Http.HttpRequest
{
Method = HttpMethod.Post,
Address = "http://www.fayelf.com",
Data=new Dictionary<string, string>
{
{"account","jacky" },
{"password","123456" }
}
});
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
/*请求成功*/
//响应内容
var value = result.Html;
//响应内容字节集
var bytes = result.Data;
}
else
{
/*请求失败*/
}
- POST BODY请求
var result = await XiaoFeng.Http.HttpHelper.GetHtmlAsync(new XiaoFeng.Http.HttpRequest
{
Method = HttpMethod.Post,
ContentType="application/json",
Address = "http://www.fayelf.com",
BodyData=@"{""account"":""jacky"",""password"":""123456""}"
});
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
/*请求成功*/
//响应内容
var value = result.Html;
//响应内容字节集
var bytes = result.Data;
}
else
{
/*请求失败*/
}
- POST FORMDATA 请求,就是有表单输入数据也有文件流数据
var result = await XiaoFeng.Http.HttpHelper.GetHtmlAsync(new XiaoFeng.Http.HttpRequest
{
Method = HttpMethod.Post,
ContentType = "application/x-www-form-urlencoded",
Address = "http://www.fayelf.com",
FormData = new List<XiaoFeng.Http.FormData>
{
new XiaoFeng.Http.FormData
{
Name="account",Value="jacky", FormType= XiaoFeng.Http.FormType.Text
},
new XiaoFeng.Http.FormData
{
Name="password",Value="123456",FormType= XiaoFeng.Http.FormType.Text
},
new XiaoFeng.Http.FormData
{
Name="headimage",Value=@"E://Work/headimage.png", FormType= XiaoFeng.Http.FormType.File
}
}
});
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
/*请求成功*/
//响应内容
var value = result.Html;
//响应内容字节集
var bytes = result.Data;
}
else
{
/*请求失败*/
}
- 下载文件
await XiaoFeng.Http.HttpHelper.Instance.DownFileAsync(new XiaoFeng.Http.HttpRequest
{
Method = HttpMethod.Get,
Address = "http://www.fayelf.com/test.rar"
}, @"E:/Work/test.rar");