我有一个使用 SharePoint 2010 REST API 的应用程序。 在创建一个 Item 的过程中,有多个请求相继完成:
1 Call: Getting Items from List: Succes
2 Call: Create Item: 401 Unauthorized
如果我这样做,效果是一样的:
1 Call: Create Item: Succes
2 Call: Delete Item: 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;
}
最佳答案
刚刚遇到同样的问题。
无论如何,第二个请求不遵循相同的身份验证过程。即使您初始化一个新的 HttpClient 对象。我嗅探了 HTTP 流量。
在第一个请求之后,我正在使用不同的凭据执行另一个请求。这也是以 401 结尾。我真的很困惑......
NTLM 握手似乎卡在 6 个步骤中的第 2 个步骤 http://www.innovation.ch/personal/ronald/ntlm.html
编辑: 您可能想要使用 CSOM。 http://social.msdn.microsoft.com/Forums/office/en-US/efd12f11-cdb3-4b28-a9e0-32bfab71a419/windows-phone-81-sdk-for-sharepoint-csom?forum=sharepointdevelopment
关于c# - 401 第二次 HttpClient/HttpWebRequest 调用未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24922929/