|
@@ -393,60 +393,98 @@ namespace molilian.core
|
|
|
return result;
|
|
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
|
|
#if DEBUG
|
|
|
- client.Proxy = null;
|
|
|
|
|
|
|
+ client.Proxy = null;
|
|
|
#endif
|
|
#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();
|
|
var root = body.Convert2JsonElement();
|
|
|
int code = root.Read<int>("code");
|
|
int code = root.Read<int>("code");
|
|
|
string message = root.Read<string>("message");
|
|
string message = root.Read<string>("message");
|
|
|
string shortCode = root.PathRead<string>("data.shortCode");
|
|
string shortCode = root.PathRead<string>("data.shortCode");
|
|
|
|
|
|
|
|
|
|
+ result.content = wareUrl;
|
|
|
if (code == 200)
|
|
if (code == 200)
|
|
|
{
|
|
{
|
|
|
result.success = true;
|
|
result.success = true;
|
|
|
result.message = "OK";
|
|
result.message = "OK";
|
|
|
result.commercial = true;
|
|
result.commercial = true;
|
|
|
result.shortLinkurl = shortCode;
|
|
result.shortLinkurl = shortCode;
|
|
|
- result.content = result.shortLinkurl;
|
|
|
|
|
result.deeplink_url = GetDefaultStyleDeeplink(result.shortLinkurl);
|
|
result.deeplink_url = GetDefaultStyleDeeplink(result.shortLinkurl);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- if (string.IsNullOrEmpty(body)) message = "网络异常";
|
|
|
|
|
|
|
+ string reason = message;
|
|
|
|
|
+ if (string.IsNullOrEmpty(body))
|
|
|
|
|
+ {
|
|
|
|
|
+ message = "网络异常";
|
|
|
|
|
+ reason = $"{response?.ResponseMessage?.StatusCode}";
|
|
|
|
|
+ }
|
|
|
result.success = false;
|
|
result.success = false;
|
|
|
result.message = "转链失败";
|
|
result.message = "转链失败";
|
|
|
- result.reason = message;
|
|
|
|
|
|
|
+ result.reason = reason;
|
|
|
result.shortLinkurl = wareUrl;
|
|
result.shortLinkurl = wareUrl;
|
|
|
result.deeplink_url = GetDefaultStyleDeeplink(result.shortLinkurl);
|
|
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;
|
|
return result;
|
|
|
}
|
|
}
|