簡體   English   中英

使用多個字符串參數調用Post Web API?

[英]Call a Post Web API with multiple string parameters?

我們如何調用以下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;

編輯:
我不確定網址是否正確,但顯示404 (Not Found)

我確實在類似情況下為我這樣做:

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;

        ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM