一、UploadData方法(Content-Type:application/x-www-form-urlencoded)web
//建立WebClient 对象
WebClient webClient = new WebClient();
//地址
string path = "http://******";
//须要上传的数据
string postString = "username=***&password=***&grant_type=***";
//以form表单的形式上传
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
// 转化成二进制数组
byte[] postData = Encoding.UTF8.GetBytes(postString);
// 上传数据
byte[] responseData = webClient.UploadData(path, "POST", postData);
//获取返回的二进制数据
string result = Encoding.UTF8.GetString(responseData);json
二、UploadData方法(Content-Type:application/json)数组
//建立WebClient 对象
WebClient webClient = new WebClient();
//地址
string path = "http://******";
//须要上传的数据
string jsonStr = "{\"pageNo\":1,\"pageSize\":3,\"keyWord\":\"\"}";app
//若是调用的方法须要身份验证则必须加以下请求标头
string token = "eyJhbGciOiJSUzI..................";
webClient.Headers.Add(HttpRequestHeader.Authorization, $"Bearer {token}");post
//或者webClient.Headers.Add("Authorization", $"Bearer {token}");url
//以json的形式上传
webClient.Headers.Add("Content-Type", "application/json");
// 转化成二进制数组
byte[] postData = Encoding.UTF8.GetBytes(jsonStr);
// 上传数据
byte[] responseData = webClient.UploadData(path, "POST", postData);
//获取返回的二进制数据
string result = Encoding.UTF8.GetString(responseData);
spa
三、DownloadData方法code
WebClient webClient = new WebClient();
string path = "http://******";orm
//若是调用的方法须要身份验证则必须加以下请求标头
string token = "eyJhbGciOiJSUzI1NiIs.........";
webClient.Headers.Add(HttpRequestHeader.Authorization, $"Bearer {token}");对象
// 下载数据
byte[] responseData = webClient.DownloadData(path);
string result = Encoding.UTF8.GetString(responseData);
四、DownloadString方法
//建立WebClient 对象
WebClient webClient = new WebClient();
//地址
string path = "http://******";
//若是调用的方法须要身份验证则必须加以下请求标头
string token = "eyJhbGciOiJSUzI1NiIsI.................";
//设置请求头--名称/值对
webClient.Headers.Add(HttpRequestHeader.Authorization, $"Bearer {token}");
//设置请求查询条件--名称/值对
webClient.QueryString.Add("type_S", "个人类型");
// 下载数据 string responseData = webClient.DownloadString(path);