Http/Https请求
- 发送请求必要参数
- 协议头参数
- RestSharp连接
发送请求必要参数
1.Request URL:https://www.baidu.com/ 请求服务地址
2.Request Method: GET 请求方式(常用方式:Post/Get)
协议头参数
1.content-encoding: gzip 压缩模式
2.content-type: text/html; charset=utf-8 连接类型和编码
3.date: Fri, 29 May 2020 03:03:42 GMT 发送数据时间
4.server: nginx 服务器系统
5.set-cookie: xHPy_2132_lastact=1590721422%09member.php%09logging; 携带的cookie
RestSharp连接
1.Post方式
一般用于账号的登录,文件的上传等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | using System; using RestSharp; namespace RestSharpHttp { class RestPost { /// <summary> /// Post提交 /// </summary> /// <param name="url">提交地址</param> /// <param name="content">提交的Post信息</param> /// <param name="contentType">提交信息格式类型</param> /// <returns></returns> public string HttpPost(string url,string content,string contentType) { try { var client = new RestClient(url); var request = new RestRequest(Method.POST); request.Timeout = 5000; request.AddParameter(contentType, content, ParameterType.RequestBody); IRestResponse response = client.Execute(request); return response.Content; } catch (Exception ex) { return ex.ToString(); } } } } |
2.Get方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System; using RestSharp; namespace RestSharpHttp { class RestPost { /// <summary> /// Get提交 /// </summary> /// <param name="url">提交地址</param> /// <returns></returns> public string HttpGet(string url) { try { var client = new RestClient(url); var request = new RestRequest(Method.GET); request.Timeout = 5000; request.AddHeader("content-type", "text/html; charset=utf-8"); request.AddHeader("content-encoding", "gzip"); IRestResponse response = client.Execute(request); return response.Content; } catch (Exception ex) { return ex.ToString (); } } } } |