2016-10-03 22 views

回答

0

您應該使用HTTP POST

using System.Net.Https; // Add this library 

using (var client = new HttpClient()) 
{ 
    var values = "DataToSend"; 

    var content = new FormUrlEncodedContent(values); 
    var response = await client.PostAsync("http://sit.com/sample.aspx", content); 

    var responseString = await response.Content.ReadAsStringAsync(); //JSON 
} 
0

可以調用使用的HttpClient(Microsoft.AspNet.WebApi.Client)任何REST API

以下方法返回一個JSON字符串異步可以保存以後

static async Taskstring> GetContentAsync(string path) 
{ 
using (var httpClient = new HttpClient()) 
return await httpClient.GetStringAsync(address); 
} 

得到JSON內容同步

using (var client = new HttpClient()) 
{ 
    var response = client.GetAsync("http://example.com").Result; 

    if (response.IsSuccessStatusCode) 
    {  

     string responseString = response.Content.ReadAsStringAsync().Result; 

     Console.WriteLine(responseString);//you can save the responseString here 
    } 
} 

,或者使用開放源碼庫等RestSharp

var client = new RestClient("http://example.com");   
// execute the request 
IRestResponse response = client.Execute(request); 
var content = response.Content; // raw content as string 

更多實例可以發現https://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client或使用restsharp

相關問題