c# - 带 cookie 的重定向 WebRequest 不起作用(Windows 内部版本 15063)

标签 c# uwp windows-10-mobile

我创建了一个调用 Web 服务的 UWP 应用程序,该 Web 服务通过结合了 cookie 的重定向进行响应。

这适用于 Windows 10 版本 1803(内部版本 17134)
当切换到 Windows 10 Creators Update(内部版本 15063)以便它将在 Windows 10 Mobile 上运行时它停止工作(在 PC 和移动设备上)

使用 fiddler 可以看到在重定向请求时不再使用 cookie。

public static async System.Threading.Tasks.Task<double> GetCreditAsync(string number, string pun, System.Threading.CancellationToken cancel = default(System.Threading.CancellationToken))
{
    var cookieContainer = new CookieContainer();

    var request = System.Net.WebRequest.Create("http://test.test") as HttpWebRequest;
    using (cancel.Register(() => request.Abort(), useSynchronizationContext: false))
    {
        request.Method = "POST";
        request.CookieContainer = cookieContainer;
        
        request.ContentType = "multipart/form-data; boundary=---------------------------7e23ca1f27119e";
        var data = "-----------------------------7e23ca1f27119e"
        + "\n" + "Content-Disposition: form-data; name=\"data1\""
        + "\n" + ""
        + "\n" + number
        + "\n" + "-----------------------------7e23ca1f27119e"
        + "\n" + "Content-Disposition: form-data; name=\"data2\""
        + "\n" + ""
        + "\n" + pun
        + "\n" + "-----------------------------7e23ca1f27119e--"
        + "\n" + "";
        var buffer = System.Text.Encoding.UTF8.GetBytes(data);
        using (var requeststream = await request.GetRequestStreamAsync())
            requeststream.Write(buffer, 0, buffer.Length);

        using (var response = (await request.GetResponseAsync()) as HttpWebResponse)
        {
            using (var responseStream = response.GetResponseStream())
            using (var stream = new StreamReader(responseStream))
            {
                var text = await stream.ReadToEndAsync();
                value = GetValue(text);
                return value;
            }
        }
    }
}

知道如何让它在 Windows Phone 上运行吗?

最佳答案

我发现 HttpClient 有一个采用 HttpMessageHandler 的构造函数。虽然 HttpRequest (还) 没有禁用自动重定向的选项,但这可以在 HttpClientHandler 上设置。

var cookieContainer = new CookieContainer();
using (var client = new HttpClient(new HttpClientHandler()
{
    CookieContainer = cookieContainer,
    UseCookies = true,
    AllowAutoRedirect = false
}))
{
    var content = new MultipartFormDataContent
        {
            { new StringContent(number), "data1" },
            { new StringContent(pun), "data2" },
        };

    var result = await client.PostAsync(RequestUriString, content, cancel);
    result = await client.GetAsync(RequestUriString, cancel);


    var text = await result.Content.ReadAsStringAsync();
    // ...
 }

关于c# - 带 cookie 的重定向 WebRequest 不起作用(Windows 内部版本 15063),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50914672/

相关文章:

c# - 根据节点 ID 创建浏览路径

c# - 单选按钮列表文本不会一直向右对齐 ASP.NET C#

c# - 通过带有异步的 Entity Framework 运行存储过程是否会忽略 context.timeout

c# - .netstandard 库和 UniversalApiContract

c# - 使用流保存 xml 文件会导致重复的根元素

uwp - 如何防止在 UWP 中的任务切换器中显示

c# - 将 net framework 4.5.2 升级到 4.7.1 后生成错误 "An attempt was made to load an assembly with an incorrect format "

c++ - 在 Windows 10 通用应用程序 UWP 上覆盖关闭框

c# - XDocument 获取定义它自己的命名空间的元素

c# - 应用第二次运行时拒绝访问文件