云图库-以图搜图失效解决
▼java复制代码@Slf4j public class GetImagePageUrlApi { /** * 获取图片页面地址 * * @param imageUrl * @return */ public static String getImagePageUrl(String imageUrl) { // 1. 准备请求参数 Map<String, Object> formData = new HashMap<>(); formData.put("image", imageUrl); formData.put("tn", "pc"); formData.put("from", "pc"); formData.put("image_source", "PC_UPLOAD_URL"); // 获取当前时间戳 long uptime = System.currentTimeMillis(); // 请求地址 String url = "https://graph.baidu.com/upload?uptime=" + uptime; try { // 2. 发送 POST 请求到百度接口 HttpResponse response = HttpRequest.post(url) .form(formData) .timeout(5000) .execute(); // 判断响应状态 if (HttpStatus.HTTP_OK != response.getStatus()) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "接口调用失败"); } // 解析响应 String responseBody = response.body(); Map<String, Object> result = JSONUtil.toBean(responseBody, Map.class); // 3. 处理响应结果 if (result == null || !Integer.valueOf(0).equals(result.get("status"))) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "接口调用失败"); } Map<String, Object> data = (Map<String, Object>) result.get("data"); String rawUrl = (String) data.get("url"); // 对 URL 进行解码 String searchResultUrl = URLUtil.decode(rawUrl, StandardCharsets.UTF_8); // 如果 URL 为空 if (searchResultUrl == null) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "未返回有效结果"); } return searchResultUrl; } catch (Exception e) { log.error("搜索失败", e); throw new BusinessException(ErrorCode.OPERATION_ERROR, "搜索失败"); } } public static void main(String[] args) { // 测试以图搜图功能 String imageUrl = "https://www.codefather.cn/logo.png"; String result = getImagePageUrl(imageUrl); System.out.println("搜索成功,结果 URL:" + result); } }
原来的代码请求会被拒绝,参考这篇文章:https://www.codefather.cn/post/1892243198542032897
加上 Acs-Token 字段去请求,发现还是拒绝
▼java复制代码@Slf4j public class GetImagePageUrlApi { private static final String ACS_TOKEN = "1781414193742_1781484661412_B/FeawyvLLcVmoTxFS4f+v4brUzlXmzQD+EoUTk7NDyPpGMI+nF/YCa8JhYgXqTybW5S3uxfqQKp2m41y26ilXE57gd5vX0T6gb69u4P+HIcTwaiSdjVpxaB7DcxjsUFYysLB7QBgRPopl6GRHrGuvP+JS7tMn42kTmOoBbDg2arzdouZzelxLXjpCDbKCyJFK2TEwLrQTDqrPnkkudrE54KStdUj1ExQ2Hf3C2kC0q5Q1iemaezaxssutGYGaVxltTXDT5+9JimtHyPcuPYYrJtQSoX7qyN5PEajAop2vUDczktBuaG/yyLX+vTrg7kPMI3TMP1sy7ne4R7XMvziKj/Pqw8l/T9pFLlMuSQ+bKnRc6suyAYa+LFbdBpgwDOep03D4Xlj3ETVm9pzmiC7GAe8FzE/FdcGITrT76OWtY="; /** * 获取以图搜图页面地址 * * @param imageUrl 图片url * @return 搜索结果url */ public static String getImagePageUrl(String imageUrl) { // 1. 准备请求参数 Map<String, Object> formData = new HashMap<>(); formData.put("image", imageUrl); formData.put("tn", "pc"); formData.put("from", "pc"); formData.put("image_source", "PC_UPLOAD_URL"); // 获取当前时间戳 long uptime = System.currentTimeMillis(); // 请求地址 String url = "https://graph.baidu.com/upload?uptime=" + uptime; try { // 2. 发送 POST 请求到百度接口 HttpResponse response = HttpRequest.post(url) .form(formData) .header("Acs-Token", ACS_TOKEN) .timeout(5000) .execute(); // 判断响应状态 if (HttpStatus.HTTP_OK != response.getStatus()) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "接口调用失败"); } // 解析响应 String responseBody = response.body(); Map<String, Object> result = JSONUtil.toBean(responseBody, Map.class); // 处理响应结果 if (result == null || !Integer.valueOf(0).equals(result.get("status"))) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "接口调用失败"); } Map<String, Object> data = (Map<String, Object>) result.get("data"); String rawUrl = (String) data.get("url"); // 对 URL 进行解码 String searchResultUrl = URLUtil.decode(rawUrl, StandardCharsets.UTF_8); // 如果url 为空 if (searchResultUrl == null) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "未返回有效结果"); } return searchResultUrl; } catch (Exception e) { log.error("搜索失败", e); throw new BusinessException(ErrorCode.OPERATION_ERROR, "搜索失败"); } } public static void main(String[] args) { // 测试意图搜索功能 String imageUrl = "https://haowallpaper.com/link//common/file/previewFileImg/19095722698298240"; String result = getImagePageUrl(imageUrl); System.out.println("搜索成功,结果 URL: " + result); } }

参考这篇文章https://www.codefather.cn/post/2044002849209217025

发现是把所有浏览器的请求头参数加上,我太懒了,不想加,观察到 Acs-Token 为 空字符串
尝试了一下,发现: 只需要 空字符就可以请求成功

完整代码
▼java复制代码import cn.hutool.core.util.URLUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpStatus; import cn.hutool.json.JSONUtil; import com.my.picturesystembackend.exception.BusinessException; import com.my.picturesystembackend.exception.ErrorCode; import lombok.extern.slf4j.Slf4j; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @Slf4j public class GetImagePageUrlApi { /** * 获取以图搜图页面地址 * * @param imageUrl 图片url * @return 搜索结果url */ public static String getImagePageUrl(String imageUrl) { // 1. 准备请求参数 Map<String, Object> formData = new HashMap<>(); formData.put("image", imageUrl); formData.put("tn", "pc"); formData.put("from", "pc"); formData.put("image_source", "PC_UPLOAD_URL"); // 获取当前时间戳 long uptime = System.currentTimeMillis(); // 请求地址 String url = "https://graph.baidu.com/upload?uptime=" + uptime; try { // 2. 发送 POST 请求到百度接口 HttpResponse response = HttpRequest.post(url) .form(formData) .header("Acs-Token", "") .timeout(5000) .execute(); // 判断响应状态 if (HttpStatus.HTTP_OK != response.getStatus()) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "接口调用失败"); } // 解析响应 String responseBody = response.body(); Map<String, Object> result = JSONUtil.toBean(responseBody, Map.class); // 处理响应结果 if (result == null || !Integer.valueOf(0).equals(result.get("status"))) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "接口调用失败"); } Map<String, Object> data = (Map<String, Object>) result.get("data"); String rawUrl = (String) data.get("url"); // 对 URL 进行解码 String searchResultUrl = URLUtil.decode(rawUrl, StandardCharsets.UTF_8); // 如果url 为空 if (searchResultUrl == null) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "未返回有效结果"); } return searchResultUrl; } catch (Exception e) { log.error("搜索失败", e); throw new BusinessException(ErrorCode.OPERATION_ERROR, "搜索失败"); } } public static void main(String[] args) { // 测试意图搜索功能 String imageUrl = "https://haowallpaper.com/link//common/file/previewFileImg/19095722698298240"; String result = getImagePageUrl(imageUrl); System.out.println("搜索成功,结果 URL: " + result); } }
技巧
-
F12 观察接口的响应结果,显示不出来,换上火狐,我也显示不出来(doge)
-
请求该接口页面会进行刷新,导致网络控制台的内容被清空,开启如下图这个可以防止清空而看不到请求。

封面

评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
