windows phone 8.1 call rest api c#
我正在开发一个windows phone 8.1
应用程序。在这个应用程序中,我需要调用宁静的apis。这里是我的API调用方法windows phone 8.1 call rest api c#
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?
在对错误的描述中,它表示您尚未将名为System.Net.Http
的程序集添加到您的参考中。只要尝试右键点击应用的参考资料[图片1],然后选择Add reference
即可。在框架列表中找到System.Net.Http
并将其添加到您的项目中[image 2]。
[下编辑HERE]
自认为不是问题,我已经把它调用REST API(异步)的方法的一个小例子。让我知道这是否适合你,并且如果不提供关于API的更多细节。
public async Task<string> callRestAPI(string serviceURL)
{
var getRequest = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, serviceURL);
var postRequest = new System.Net.Http.HttpRequestMessage(HttpMethod.Post, serviceURL);
postRequest.Content = new StringContent("string data");
using (var httpClient = new System.Net.Http.HttpClient())
{
var getResponse = await httpClient.SendAsync(getRequest);
var postResponse = await httpClient.SendAsync(postRequest);
var getData = await getResponse.Content.ReadAsStringAsync();
var postData = await postResponse.Content.ReadAsStringAsync();
Debug.WriteLine(getData);
Debug.WriteLine(postData);
return getData;
}
}
我打电话的方法,只是在OnNavigatedTo
方法测试如下:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
// TODO: Prepare page for display here.
// TODO: If your application contains multiple pages, ensure that you are
// handling the hardware Back button by registering for the
// Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
// If you are using the NavigationHelper provided by some templates,
// this event is handled for you.
var testResult = await callRestAPI("~your URL here~");
}
您必须添加Microsoft.Net.Http
的NuGet包的Windows Phone(和其他平台一样)。你可以在这里找到包信息:
https://www.nuget.org/packages/Microsoft.Net.Http/
通过命令行或右键单击该项目中添加这,然后选择Manage NuGet Packages
,然后搜索Microsoft.Net.Http
最后选择Install
。
使用包管理器控制台,您可以使用以下命令将此包添加到您的项目中。
Install-Package Microsoft.Net.Http
我针对的是windows phone 8.1。 –
看我的编辑。让我知道它是否适用于您 – rhcpfan
感谢您的回复。我尝试过这个。但不工作。给出错误'错误CS0012:类型'TaskAwaiter '在未引用的程序集中定义。你必须添加一个引用程序集'System.Threading.Tasks,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'。' –