* Pixiview/Utils/Stores.cs: fix: gzip transmission

* Pixiview/Illust/ViewIllustPage.xaml.cs:
This commit is contained in:
Tsanie Lily 2020-05-07 22:10:30 +08:00
parent f6575f61e6
commit cae3692140
2 changed files with 41 additions and 41 deletions

View File

@ -104,6 +104,7 @@ namespace Pixiview.Illust
items[i] = new IllustDetailItem(); items[i] = new IllustDetailItem();
if (i == 0) if (i == 0)
{ {
items[i].Loading = true;
items[i].Image = illust.Image; items[i].Image = illust.Image;
} }
} }

View File

@ -1,8 +1,8 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers;
using System.Text; using System.Text;
using Newtonsoft.Json; using Newtonsoft.Json;
using Pixiview.Illust; using Pixiview.Illust;
@ -43,7 +43,7 @@ namespace Pixiview.Utils
} }
} }
private static T LoadObject<T>(string file, string url, string referer = null, bool force = false, IEnumerable<(string header, string value)> headers = null) private static T LoadObject<T>(string file, string url, string referer = null, bool force = false)
{ {
string content = null; string content = null;
if (!force && File.Exists(file)) if (!force && File.Exists(file))
@ -59,7 +59,14 @@ namespace Pixiview.Utils
} }
if (content == null) if (content == null)
{ {
var response = Download(url, referer, headers); var response = Download(url, headers =>
{
headers.Referrer = referer == null ? Configs.Referer : new Uri(referer);
headers.Add("user-agent", Configs.UserAgent);
headers.Add("accept", Configs.AcceptJson);
headers.Add("cookie", Configs.Cookie);
headers.Add("x-user-id", Configs.UserId);
});
if (response == null) if (response == null)
{ {
return default; return default;
@ -157,12 +164,7 @@ namespace Pixiview.Utils
var result = LoadObject<IllustData>( var result = LoadObject<IllustData>(
file, file,
Configs.UrlIllustList, Configs.UrlIllustList,
force: force, force: force);
headers: new[]
{
("cookie", Configs.Cookie),
("x-user-id", Configs.UserId)
});
return result; return result;
} }
@ -173,12 +175,7 @@ namespace Pixiview.Utils
file, file,
string.Format(Configs.UrlIllustPage, id), string.Format(Configs.UrlIllustPage, id),
string.Format(Configs.UrlIllust, id), string.Format(Configs.UrlIllust, id),
force: force, force: force);
headers: new[]
{
("cookie", Configs.Cookie),
("x-user-id", Configs.UserId)
});
return result; return result;
} }
@ -259,7 +256,12 @@ namespace Pixiview.Utils
} }
var file = Path.Combine(directory, Path.GetFileName(url)); var file = Path.Combine(directory, Path.GetFileName(url));
App.DebugPrint($"download, url: {url}"); App.DebugPrint($"download, url: {url}");
var response = Download(url); var response = Download(url, headers =>
{
headers.Referrer = Configs.Referer;
headers.Add("user-agent", Configs.UserAgent);
headers.Add("accept", Configs.AcceptImage);
});
if (response == null) if (response == null)
{ {
return null; return null;
@ -301,14 +303,15 @@ namespace Pixiview.Utils
return default; return default;
} }
private static HttpResponseMessage Download(string url, string referer = null, IEnumerable<(string header, string value)> headers = null) private static HttpResponseMessage Download(string url, Action<HttpRequestHeaders> headerAction)
{ {
App.DebugPrint($"GET: {url}"); App.DebugPrint($"GET: {url}");
var uri = new Uri(url); var uri = new Uri(url);
var handler = new HttpClientHandler var handler = new HttpClientHandler
{ {
Proxy = Configs.Proxy, Proxy = Configs.Proxy,
UseProxy = true UseProxy = true,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
}; };
var client = new HttpClient(handler) var client = new HttpClient(handler)
{ {
@ -321,15 +324,10 @@ namespace Pixiview.Utils
Version = new Version(2, 0) Version = new Version(2, 0)
}) })
{ {
request.Headers.Referrer = referer == null ? Configs.Referer : new Uri(referer); var headers = request.Headers;
request.Headers.Add("user-agent", Configs.UserAgent); headerAction(headers);
if (headers != null) headers.Add("accept-language", Configs.AcceptLanguage);
{ headers.Add("accept-encoding", Configs.AcceptEncoding);
foreach (var (header, value) in headers)
{
request.Headers.Add(header, value);
}
}
return client.SendAsync(request).Result; return client.SendAsync(request).Result;
} }
}); });
@ -342,24 +340,25 @@ namespace Pixiview.Utils
public static readonly Uri Referer = new Uri("https://www.pixiv.net/"); public static readonly Uri Referer = new Uri("https://www.pixiv.net/");
public const int MaxThreads = 3; public const int MaxThreads = 3;
public const string UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36";
public const string UrlIllustList = "https://www.pixiv.net/ajax/top/illust?mode=all&lang=zh"; public const string UrlIllustList = "https://www.pixiv.net/ajax/top/illust?mode=all&lang=zh";
public const string UrlIllust = "https://www.pixiv.net/artworks/{0}"; public const string UrlIllust = "https://www.pixiv.net/artworks/{0}";
public const string UrlIllustPage = "https://www.pixiv.net/ajax/illust/{0}/pages?lang=zh"; public const string UrlIllustPage = "https://www.pixiv.net/ajax/illust/{0}/pages?lang=zh";
public const string Cookie = "first_visit_datetime_pc=2019-10-29+22%3A05%3A30; p_ab_id=2; p_ab_id_2=6;" + public const string UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36";
" p_ab_d_id=1155161977; a_type=0; b_type=1; d_type=2; module_orders_mypage=%5B%7B%22name%22%3A%22s" + public const string AcceptImage = "image/png,image/svg+xml,image/*;q=0.8,video/*;q=0.8,*/*;q=0.5";
"ketch_live%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22tag_follow%22%2C%22visible%22%3Atrue" + public const string AcceptJson = "application/json";
"%7D%2C%7B%22name%22%3A%22recommended_illusts%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22ev" + public const string AcceptEncoding = "gzip, deflate";
"eryone_new_illusts%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22following_new_illusts%22%2C%" + public const string AcceptLanguage = "zh-cn";
"22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22mypixiv_new_illusts%22%2C%22visible%22%3Atrue%7D%2C%7" +
"B%22name%22%3A%22spotlight%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22fanbox%22%2C%22visib" +
"le%22%3Atrue%7D%2C%7B%22name%22%3A%22featured_tags%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3" +
"A%22contests%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22user_events%22%2C%22visible%22%3At" +
"rue%7D%2C%7B%22name%22%3A%22sensei_courses%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22boot" +
"h_follow_items%22%2C%22visible%22%3Atrue%7D%5D; yuid_b=NgcXQWQ; login_ever=yes; c_type=31; PHPSES" +
"SID=2603358_VHyGPeRaz7LpeoFkRsHvjXIpApCMb56a; __cfduid=d9fa2d4d1ddd30db85ebb519f9855d256158780674" +
"7; privacy_policy_agreement=2";
public const string UserId = "2603358"; public const string UserId = "2603358";
public const string Cookie =
"PHPSESSID=2603358_VHyGPeRaz7LpeoFkRsHvjXIpApCMb56a; " +
"a_type=0; b_type=1; c_type=31; d_type=2; " +
"p_ab_id=2; p_ab_id_2=6; p_ab_d_id=1155161977; " +
"privacy_policy_agreement=2; " +
"login_ever=yes; " +
"__cfduid=d9fa2d4d1ddd30db85ebb519f9855d2561587806747; " +
"first_visit_datetime_pc=2019-10-29+22%3A05%3A30; " +
"yuid_b=NgcXQWQ";
public static string GetThumbnailUrl(string url) public static string GetThumbnailUrl(string url)
{ {