EasyHttp基本使用
来源:互联网 发布:许小年水平怎么样知乎 编辑:程序博客网 时间:2024/05/16 19:49
由于在用C#请求DRF的API时遇到了认证问题,只好用个第三方的包来解决这个问题。
同时了解到了HttpLib和EasyHttp,然后决定使用EasyHttp。
项目的地址:https://github.com/EasyHttp/EasyHttp。在VS上可以直接用NuGet安装。
主要使用的内容在EasyHttp.Http中。
using EasyHttp.Http;下面是一个get请求的示例:
HttpClient client = new HttpClient();var response = client.Get("http://127.0.0.1:8000/");Console.WriteLine(response.StatusCode);Console.WriteLine(response.RawText);要建立一个请求客户端,就使用HttpClient类。
之后调用该对象的Get/Post/Put/Patch/Pelete等方法发送一个请求,并返回一个HttpResponse对象。
不同的请求方法需要传入的参数不同。
HttpResponse Get(string uri) //GET请求,传入URLHttpResponse Head(string uri) //HEAD请求HttpResponse Options(string uri) //OPTIONS请求HttpResponse Post(string uri, string content, string contentType) //POST请求之一,传入内容字符串content,同时需要一个contentType类型HttpResponse Post(string uri, IDictionary<string,object> data) //POST请求之二,传入一个POST内容的字典HttpResponse Put(string uri, string content, string contentType) //PUT请求HttpResponse Patch(string uri, string content, string contentType) //PATCH请求HttpResponse Delete(string uri) //DELETE请求
HttpResponse对象包含response的所有信息,其中比较重要的有:
response.ContentType; //ContentTyperesponse.Cookies; //Cookiesresponse.RawText; //Contentresponse.StatusCode; //HTTP状态码response.StatusDescription; //HTTP状态描述response.RawHeaders; //Headersresponse.CharacterSet; //字符集另外,如果需要访问request的信息,使用HttpClient的request属性。该属性返回一个HttpRequest对象。
如果需要设置认证功能,那么通过HttpRequest完成。
request.SetBasicAuthentication(username, password);request.ForceBasicAuth = true;
1 0
- EasyHttp基本使用
- EasyHttp 基于OkHttp的网络请求框架
- 基本使用
- Fresco基本使用基本介绍
- recyclerview基本使用使用
- Git使用-基本使用
- ANT 基本使用教程
- DUnit基本使用
- Ant 的基本使用
- Ant 的基本使用
- gnuplot基本使用(kelly)
- Eclipse的基本使用
- Eclipse的基本使用
- AJAXDWR基本使用向导
- JBuilder的基本使用
- sqlplus基本使用
- List Control 基本使用
- csc基本使用
- postman接口测试
- 【C#】访问泛型中的List列表数据
- Hadoop之HDFS的配置------HA(高可用版本)
- HDOJ 4507 吉哥系列故事——恨7不成妻(数位DP)
- 产品的本质,到底是个什么东西?
- EasyHttp基本使用
- 【Android 基础】Animation 动画介绍和实现
- 数据结构——线性表——知识点总结
- linux 环境变量
- 利用Android Studio打开Hierarchy window
- 关于接收到推送下来的通知禁止声音和振动的问题 JPushInterface.setPushNotificationBuilder(1, builder);
- word2010首行缩进、设置页码为第几页共几页、图片完全不显示等问题
- 支付宝刮奖案例
- nginx 反向代理测试