一、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);