Я работаю над приложением для Windows, и у меня возникают проблемы с файлами cookie. Обратите внимание, что я работаю с Windows.Web.Http, а не с пространством имен System HttpClient.
API, с которым я работаю, использует для аутентификации заголовок авторизации. По сути, после POST для входа в систему мне нужен способ вернуть файлы cookie, а затем использовать эти файлы cookie для выполнения последующих вызовов API. Я разместил пример того, что у меня есть в настоящее время, и это удается. Я вижу файлы cookie в объекте результата. Я просто не совсем уверен, куда идти отсюда / как действовать дальше. Спасибо! Любые идеи?
using MyApi.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Web.Http;
using Newtonsoft.Json;
using MyApi.Models.Auth;
using MyApi.Models;
namespace MyApi
{
public class MyService
{
private const string MyBaseUrl = "http://api.my.com:3000";
private readonly HttpClient _httpClient = new HttpClient();
public async Task<SignInResponse> AttemptLogin(string username, string password)
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
throw new ArgumentException("Username or password is null or empty");
var uri = new Uri(string.Format("{0}/{1}", MyBaseUrl, "auth/signin"));
var authSignIn = new Models.Auth.SignInRequest();
authSignIn.Email = username;
authSignIn.Password = password;
var myObject = JsonConvert.SerializeObject(authSignIn);
// I see the headers in the result object, but I'm not
// sure the best way to a) get them out and b) shove them into
// all of the next calls
var result = await _httpClient.PostAsync(uri,
new HttpStringContent(myObject.ToString(),
Windows.Storage.Streams.UnicodeEncoding.Utf8,
"application/json"));
var content = await result.Content.ReadAsStringAsync();
var successResponse = new SignInResponse();
try
{
successResponse = JsonConvert.DeserializeObject<SignInResponse>(content);
}
catch (Exception)
{
var failResponse = JsonConvert.DeserializeObject<ErrorResponse>(content);
throw new Exception(failResponse.message);
}
return successResponse;
}
}
}
CookieContainer
в следующем сообщении... stackoverflow.com/questions/12373738/ - person 3-14159265358979323846264   schedule 10.07.2015