2013-12-08 50 views
1

我們如何調用下面的web api?用多個字符串參數調用Post Web API?

[HttpPost] 
public bool ValidateAdmin(string username, string password) 
{ 
    return _userBusinessObject.ValidateAdmin(username, password); 
} 

我已經寫了下面的代碼,但它這麼想的工作404 (Not Found)

string url = string.Format("api/User/ValidateAdmin?password={0}", password); 
HttpResponseMessage response = Client.PostAsJsonAsync(url, username).Result; 
response.EnsureSuccessStatusCode(); 
return response.Content.ReadAsAsync<bool>().Result; 

編輯:
我死了肯定的URL,但它說404 (Not Found)

回答

2

我這樣做對我來說很相似:

MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter(); 
HttpContent datas = new ObjectContent<dynamic>(new { 
    username= username, 
    password= password}, jsonFormatter); 

var client = new HttpClient(); 

client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json")); 

var response = client.PostAsync("api/User/ValidateAdmin", datas).Result; 

if (response != null) 
{ 
    try 
    { 
     response.EnsureSuccessStatusCode(); 
     return response.Content.ReadAsAsync<bool>().Result; 

     ...