C# .Net EasyHttp (Http Library for C#) 应用实例

一、前文

EasyHttp/EasyHttp:Http Library for C#

An easy to use HTTP client that supports:

  • HEAD, PUT, DELETE, GET, POST
  • Cookies
  • Authentication
  • Dynamic and Static Typing
  • XML, JSON and WWW-Url form encoded encoding/decoding
  • File upload both via PUT and POST (multipart/formdata)
  • Some other neat little features…

二、Get实例

2.1 Get不带参数实例

class CaptchaImageInfo
{
    public string uuid;
    public string img;
}
HttpClient http = new HttpClient();
http.Request.Accept = HttpContentTypes.ApplicationJson;
http.Request.ContentType = HttpContentTypes.ApplicationJson;
var response = http.Get("http://localhost:8080/captchaImage");
var captchaImageInfo = response.StaticBody<CaptchaImageInfo>();
uuid = captchaImageInfo.uuid;
Console.WriteLine("uuid: {0}", captchaImageInfo.uuid);
Console.WriteLine("img: {0}", captchaImageInfo.img);

2.2 Get带参数实例

class TokenInfo
{
    public string token;
    public string code;
    public string msg;
}
HttpClient http = new HttpClient();
http.Request.Accept = HttpContentTypes.ApplicationJson;
http.Request.ContentType = HttpContentTypes.ApplicationJson;
var response = http.Get("http://localhost:8080/v3/admin/system/user/login/status", new { uuid=uuid });
var tokenInfo = response.StaticBody<TokenInfo>();
Console.WriteLine("token: {0}, code: {1}, msg: {2}", tokenInfo.token, tokenInfo.code, tokenInfo.msg);

三、Post实例

class AccountLoginInfo
{
    public string username;
    public string phoneNumber;
    public string password;
    public string code;
    public string uuid;
}
HttpClient http = new HttpClient();
http.Request.Accept = HttpContentTypes.ApplicationJson;
http.Request.ContentType = HttpContentTypes.ApplicationJson;

var accountLoginInfo = new AccountLoginInfo();
accountLoginInfo.username = usernameText.Text.ToString();
accountLoginInfo.password = pwdText.Text.ToString();
accountLoginInfo.code = verificateCodeText.Text.ToString();
accountLoginInfo.uuid = uuid;

var response = http.Post("https://localhost:8080/v3/admin/system/user/login/password", accountLoginInfo, HttpContentTypes.ApplicationJson);
Console.WriteLine(response.RawText);
if (response.StatusCode != HttpStatusCode.OK)
{
    MessageBox.Show("异常");
}
var tokenInfo = response.StaticBody<TokenInfo>();
if (tokenInfo.code.Equals("200"))
{
    Console.WriteLine("token: {0}, code: {1}, msg: {2}", tokenInfo.token, tokenInfo.code, tokenInfo.msg);
    http.Request.RawHeaders.Add("Authorization", tokenInfo.token);
    MessageBox.Show("登录成功");
    this.Close();
}
else
{
    MessageBox.Show(tokenInfo.msg);
}

四、header、Authorization、token实例

http.Request.RawHeaders.Add("Authorization", token);

觉得好,就一键三连呗(点赞+收藏+关注)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小康师兄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值