Sfoglia il codice sorgente

增加jd重试、修复tb dp的2层urldecode

dodo hold 10 mesi fa
parent
commit
33d9a2f1ab

File diff suppressed because it is too large
+ 0 - 0
molilian.api/Properties/PublishProfiles/latest.pubxml.user


+ 8 - 1
molilian.core/Core/taoke/UnionParseCore/dp2dp.cs

@@ -932,7 +932,14 @@ tbopen://m.taobao.com/tbopen/index.html?h5Url=https%3A%2F%2Fku.m.taobao.com%2Fmi
             {
                 url = content.GetContentPart("h5Url=", "");
             }
-            url = url.UrlDecode();
+            if (url.StartsWith("https%253A"))
+            {
+                url = url.UrlDecode().UrlDecode();
+            }
+            if (url.StartsWith("https%253A"))
+            {
+                url = url.UrlDecode();
+            }
 
             if (string.IsNullOrEmpty(url))
             {

+ 68 - 30
molilian.core/Plus/JDUnion/goods.cs

@@ -393,60 +393,98 @@ namespace molilian.core
                 return result;
             }
 
-            WebClientUtility client = new WebClientUtility();
-            client.Proxy = _proxy;
+            // 配置重试次数(遇到 Forbidden 时重试)
+            const int maxRetryCount = 3;
+            int retryCount = 0;
+            dynamic response = null;
+            string body;
+
+            // 请求循环(首次请求 + 最多3次重试)
+            do
+            {
+                if (retryCount > 0)
+                {
+                    _ = new LoggerLibrary("JdUnion", "GetPromotionByCrawlerAsync")
+                        .Info($"Retry attempt {retryCount}/{maxRetryCount}, Previous StatusCode: {response?.ResponseMessage?.StatusCode}")
+                        .SaveAsync();
+                }
+
+                var client = new WebClientUtility();
+                client.Proxy = _proxy;
 #if DEBUG
-            client.Proxy = null;
+                client.Proxy = null;
 #endif
-            client.SetContentType("application/x-www-form-urlencoded");
-            client.AddHeaders("X-Referer-Page", "https://union.jd.com/proManager/index");
-            client.AddHeaders("X-Rp-Client", "h5_1.0.0");
-            client.AddHeaders("Referer", "https://union.jd.com/");
-            client.AddHeaders("Origin", "https://union.jd.com");
-            client.UserAgent = useragent;
-            client.AddHeaders("Cookie", cookies);
-            client.Post($"body={data}");
-            var response = await client.RequestAsync(url, "GET", cancellationToken);
-            if (!response.Successed)
-            {
-                string reason = $"{response.ResponseMessage.StatusCode} - {response.ResponseException.Message}";
-                result.success = false;
-                result.message = "转链失败";
-                result.reason = reason;
+                client.SetContentType("application/x-www-form-urlencoded");
+                client.AddHeaders("X-Referer-Page", "https://union.jd.com/proManager/index");
+                client.AddHeaders("X-Rp-Client", "h5_1.0.0");
+                client.AddHeaders("Referer", "https://union.jd.com/");
+                client.AddHeaders("Origin", "https://union.jd.com");
+                client.UserAgent = useragent;
+                client.AddHeaders("Cookie", cookies);
+                client.Post($"body={data}");
+                response = await client.RequestAsync(url, "GET", cancellationToken);
+
+                if (!response.Successed)
+                {
+                    string reason = $"{response.ResponseMessage.StatusCode} - {response.ResponseException.Message}";
+                    result.success = false;
+                    result.message = "转链失败";
+                    result.reason = reason;
+
+                    result.shortLinkurl = wareUrl;
+                    result.deeplink_url = GetDefaultStyleDeeplink(result.shortLinkurl);
+                    _ = new LoggerLibrary("JdUnion", "GetPromotionByCrawlerAsync")
+                        .Info($"{response.ResponseMessage.StatusCode} ", response.ResponseException.Message)
+                        .Info(response.ResponseException.StackTrace)
+                        .SaveAsync();
+                    return result;
+                }
 
-                result.shortLinkurl = wareUrl;
-                result.deeplink_url = GetDefaultStyleDeeplink(result.shortLinkurl);
-                _ = new LoggerLibrary("JdUnion", "GetPromotionByCrawlerAsync")
-                    .Info($"{response.ResponseMessage.StatusCode} ", response.ResponseException.Message)
-                    .Info(response.ResponseException.StackTrace)
-                    .SaveAsync();
-                return result;
+                body = response.Body();
+
+                if (retryCount > 0)
+                {
+                    _ = new LoggerLibrary("JdUnion", "GetPromotionByCrawlerAsync")
+                        .Info($"Retry {retryCount} result - StatusCode: {response?.ResponseMessage?.StatusCode}, Exception: {response?.ResponseException?.Message}")
+                        .Info(body)
+                        .SaveAsync();
+                }
+
+                retryCount++;
             }
+            while (response?.ResponseMessage?.StatusCode == HttpStatusCode.Forbidden && retryCount <= maxRetryCount);
+
 
-            string body = response.Body();
             var root = body.Convert2JsonElement();
             int code = root.Read<int>("code");
             string message = root.Read<string>("message");
             string shortCode = root.PathRead<string>("data.shortCode");
 
+            result.content = wareUrl;
             if (code == 200)
             {
                 result.success = true;
                 result.message = "OK";
                 result.commercial = true;
                 result.shortLinkurl = shortCode;
-                result.content = result.shortLinkurl;
                 result.deeplink_url = GetDefaultStyleDeeplink(result.shortLinkurl);
             }
             else
             {
-                if (string.IsNullOrEmpty(body)) message = "网络异常";
+                string reason = message;
+                if (string.IsNullOrEmpty(body))
+                {
+                    message = "网络异常";
+                    reason = $"{response?.ResponseMessage?.StatusCode}";
+                }
                 result.success = false;
                 result.message = "转链失败";
-                result.reason = message;
+                result.reason = reason;
                 result.shortLinkurl = wareUrl;
                 result.deeplink_url = GetDefaultStyleDeeplink(result.shortLinkurl);
-                _ = new LoggerLibrary("JdUnion", "GetPromotionByCrawlerAsync").Info(body).SaveAsync();
+                _ = new LoggerLibrary("JdUnion", "GetPromotionByCrawlerAsync")
+                    .Info($"StatusCode:{response?.ResponseMessage?.StatusCode}\t{response?.ResponseException?.Message}")
+                    .Info(body).SaveAsync();
             }
             return result;
         }

Some files were not shown because too many files changed in this diff