【发布时间】:2016-07-05 07:24:32
【问题描述】:
我正在开发一个windows phone 8.1 应用程序。在这个应用程序中,我需要调用 restful api。这是我的api调用方法
public static async Task<HttpResponseMessage> callrestApi(string baseurl, string api, string method, string contenttype, object objPost)
{
try
{
HttpResponseMessage response = null;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseurl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contenttype));
switch (method.ToLower())
{
case "get":
// HTTP GET
response = await client.GetAsync(api);
if (response.IsSuccessStatusCode)
{
object obj = await response.Content.ReadAsStringAsync();
//return obj;
}
break;
case "post":
// HTTP POST
response = await client.PostAsync(api, new StringContent(objPost.ToString(), Encoding.UTF8, contenttype));
if (response.IsSuccessStatusCode)
{
object obj = await response.Content.ReadAsStringAsync();
//return obj;
}
break;
default:
break;
}
}
return response;
}
catch (Exception)
{
return null;
}
}
我从 MainPage.xaml 调用此方法进行登录。我正在使用用户名和密码传递 json obj。
var resp = await Constants.callrestApi(Constants.base_Url, Constants.apiList.loginApi, Constants.httpmethod.post, Constants.contenttype.json, jsonObj);
if (resp != null)
{
}
调试时出现以下错误
错误 CS0012:“HttpResponseMessage”类型在未引用的程序集中定义。 您必须添加对程序集 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 的引用。
我认为问题出在await 电话中。我怎样才能调用restful api?
【问题讨论】:
标签: c# rest windows-phone-8.1