401未经授权在SECOND HttpClient/HttpWebRequest调用上 [英] 401 Unauthorized on SECOND HttpClient/HttpWebRequest call

查看:117
本文介绍了401未经授权在SECOND HttpClient/HttpWebRequest调用上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用SharePoint 2010 REST API的应用程序. 在创建项目的过程中,彼此之间有多个请求:

I have a application that uses the SharePoint 2010 REST API. In the process of creating an Item there are multiple request done after each other:

1次通话:从列表中获取物品:成功

1 Call: Getting Items from List: Succes

2通话:创建商品:401未经授权

2 Call: Create Item: 401 Unauthorized

如果我这样做,这是相同的:

This is the same if I do it like this:

1个呼叫:创建项目:成功

1 Call: Create Item: Succes

2通话:删除项目:401未经授权

2 Call: Delete Item: 401 Unauthorized

我所知道的是,我的函数是分开工作的,而当彼此调用它们时,不要的工作. 创建项目后关闭应用程序(Windows Phone 8.1应用程序)时,重新启动后尝试删除该项目.

What I know is that my functions work separately they DON'T work when they are called after each other. When I close the application (Windows Phone 8.1 app) after creating a item and when restarted try to delete the item it works.

首先,我认为这与处理字段的方式有关,因此我在finally语句中将它们更改为NULL,但这没用.

First I thought it had to do with the way I handle my fields so I changed them to NULL in a finally statement but that didn't work.

    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;
    }

推荐答案

只是遇到了同样的问题.

Just run into the same problem.

无论如何,第二个请求不遵循相同的身份验证过程.即使您初始化了一个新的HttpClient对象.我嗅到了HTTP流量.

Anyway, the 2nd request does not follow the same authentication procedure. Even if you initialize a new HttpClient object. I sniffed the HTTP traffic.

在发出第一个请求后,我将使用不同的凭据进行另一个操作.这也以401结尾.我真的很困惑...

After the 1st request I am doing another with different credentials. This is also ending in a 401. I am really confused...

NTLM握手卡在6个步骤中的第2个 http://www.innovation.ch/personal/ronald/ntlm.html

Seems the NTLM Handshake stucks at the 2nd of 6 steps 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

You may want to use the CSOM. http://social.msdn.microsoft.com/Forums/office/en-US/efd12f11-cdb3-4b28-a9e0-32bfab71a419/windows-phone-81-sdk-for-sharepoint-csom?forum=sharepointdevelopment

这篇关于401未经授权在SECOND HttpClient/HttpWebRequest调用上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆