401 Unauthorized on SECOND HttpClient/HttpWebReque

2019-02-20 08:33发布

问题:

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 Call: Getting Items from List: Succes

2 Call: Create Item: 401 Unauthorized

This is the same if I do it like this:

1 Call: Create Item: Succes

2 Call: Delete Item: 401 Unauthorized

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.

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

回答1:

Just run into the same problem.

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

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

Seems the NTLM Handshake stucks at the 2nd of 6 steps http://www.innovation.ch/personal/ronald/ntlm.html

Edit: 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



回答2:

While I still don't know what the actual problem is, at least I found a workaround: Use the WebRequest class instead of HttpClient.



回答3:

I see that this question has been posted long back. But I don't see a correctly working solution posted yet to this thread.

I faced exactly the same issue where the next requests kept on failing returning me 401 UnAuthorized.

I figured out using fiddler that from SECOND request onwards, there was a Cookie added to the request which was possibly a result of Set-Cookie response sent by the server along with first response.

So here's how I tackled the situation - Make UseCookies false:

new HttpClientHandler { Credentials = myCred, UseCookies = false }

This should resolve your issue. Hope this helps someone who's looking for a solution to a similar issue.