【发布时间】:2014-07-23 23:51:04
【问题描述】:
我有一个使用 SharePoint 2010 REST API 的应用程序。 在创建 Item 的过程中,有多个请求一个接一个地完成:
1 调用:从列表中获取项目:成功
2 调用:创建项目:401 Unauthorized
如果我这样做也是一样的:
1 调用:创建项目:成功
2 调用:删除项目:401 Unauthorized
我所知道的是,我的函数是单独工作的,它们不会在它们被一个接一个地调用时工作。 当我在创建项目后关闭应用程序(Windows Phone 8.1 应用程序)并在重新启动时尝试删除它工作的项目。
首先,我认为这与我处理字段的方式有关,因此我在 finally 语句中将它们更改为 NULL,但这不起作用。
public async Task<bool> CreateNewItem(NewItem myNewItem)
{
try
{
StatusBar statusBar = await MyStatusBar.ShowStatusBar("Creating new List Item.");
//Retrieving Settings from Saved file
mySettings = await MyCredentials.GetMySettings();
myCred = new NetworkCredential(mySettings.UserName, mySettings.Password, mySettings.Domain);
using (var handler = new HttpClientHandler { Credentials = myCred })
{
HttpClient client = new HttpClient(handler);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
NewItem newItem = myNewItem;
var jsonObject = JsonConvert.SerializeObject(newItem);
HttpResponseMessage response = await client.PostAsync(new Uri(baseUrl + listNameHourRegistration), new StringContent(jsonObject.ToString(), Encoding.Unicode, "application/json"));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response.EnsureSuccessStatusCode();
string responseMessage = await response.Content.ReadAsStringAsync();
client.Dispose();
if (responseMessage.Length > 0)
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return false;
}
finally
{
request = null;
response = null;
myCred = null;
mySettings = null;
}
return false;
}
【问题讨论】:
标签: c# httpwebrequest httpclient