一、前文
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);
觉得好,就一键三连呗(点赞+收藏+关注)