优化请求 header 中 Referer 规则

This commit is contained in:
cherishsince 2024-04-07 09:35:58 +08:00
parent 2c21f71327
commit defe628026
2 changed files with 9 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.framework.ai.midjourney.interactions;
import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@ -18,16 +19,18 @@ public class MjClient {
private static RestTemplate restTemplate = new RestTemplate();
private static HttpHeaders headers = new HttpHeaders();
private static final String HEADER_REFERER = "https://discord.com/channels/%s/%s";
static {
headers.setContentType(MediaType.APPLICATION_JSON); // 设置内容类型为JSON
headers.set("Referer", "https://discord.com/channels/1221445697157468200/1221445862962630706");
headers.set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36");
headers.set("Cookie", "__dcfduid=6ca536c0e3fa11eeb7cbe34c31b49caf; __sdcfduid=6ca536c1e3fa11eeb7cbe34c31b49caf52cce5ffd8983d2a052cf6aba75fe5fe566f2c265902e283ce30dbf98b8c9c93; _gcl_au=1.1.245923998.1710853617; _ga=GA1.1.111061823.1710853617; __cfruid=6385bb3f48345a006b25992db7dcf984e395736d-1712124666; _cfuvid=O09la5ms0ypNptiG0iD8A6BKWlTxz1LG0WR7qRStD7o-1712124666575-0.0.1.1-604800000; locale=zh-CN; cf_clearance=l_YGod1_SUtYxpDVeZXiX7DLLPl1DYrquZe8WVltvYs-1712124668-1.0.1.1-Hl2.fToel23EpF2HCu9J20rB4D7OhhCzoajPSdo.9Up.wPxhvq22DP9RHzEBKuIUlKyH.kJLxXJfAt2N.LD5WQ; OptanonConsent=isIABGlobal=false&datestamp=Wed+Apr+03+2024+14%3A11%3A15+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=6.33.0&hosts=&landingPath=https%3A%2F%2Fdiscord.com%2F&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A1; _ga_Q149DFWHT7=GS1.1.1712124668.4.1.1712124679.0.0.0");
}
public static String post(String url, String token, String body) {
// 设置token
headers.set("Authorization", token);
public static String post(String url, MidjourneyConfig config, String body) {
// 设置 header
headers.set("Referer", String.format(HEADER_REFERER, config.getGuildId(), config.getChannelId()));
headers.set("Authorization", config.getToken());
// 封装请求体和头部信息
HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
// 发送请求

View File

@ -40,7 +40,7 @@ public class MjInteractions {
// 设置参数
String requestBody = MjClient.setParams(requestTemplate, requestParams);
// 发送请求
String res = MjClient.post(url, midjourneyConfig.getToken(), requestBody);
String res = MjClient.post(url, midjourneyConfig, requestBody);
// 这个 res 只要不返回值就是成功!
boolean isSuccess = StrUtil.isBlank(res);
if (isSuccess) {
@ -64,7 +64,7 @@ public class MjInteractions {
// 设置参数
String requestBody = MjClient.setParams(requestTemplate, requestParams);
// 发送请求
String res = MjClient.post(url, midjourneyConfig.getToken(), requestBody);
String res = MjClient.post(url, midjourneyConfig, requestBody);
// 这个 res 只要不返回值就是成功!
boolean isSuccess = StrUtil.isBlank(res);
if (isSuccess) {