简体   繁体   English

使用多个字符串参数调用Post Web API?

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

How can we call the following web api ? 我们如何调用以下Web API?

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

I've written the following code, but it dosen't work 404 (Not Found) 我已经编写了以下代码,但无法正常工作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;

Edit: 编辑:
I'm dead sure about the Url, but it says 404 (Not Found) 我不确定网址是否正确,但显示404 (Not Found)

i do like this for me in similar case : 我确实在类似情况下为我这样做:

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