java.net HttpURLConnection内容类型不匹配

时间:2019-03-07 05:52:17

标签: java http httpurlconnection mime-types content-type

我想开发一个telegram bot来搜索iqdb.org数据库中的相关图像。我已经成功捕获了用户的图像并将其转换为链接。但是,尽管我已经建立了连接application/octet-stream

,但iqdb总是告诉我我的MIME是image/jpeg而不是con.setRequestProperty("Content-Type", "image/jpeg");

我试图在浏览器中访问文件的链接,该链接按预期工作-我的浏览器自动下载了jpeg文件。

代码:

//define the target url.
URL target = new URL("https://iqdb.org/index.xml?url=https://api.telegram.org/file/botAPITokenHidden/photos/file_1.jpg);

//establish GET connection.
HttpURLConnection con = (HttpURLConnection) target.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "image/jpeg");
con.setConnectTimeout(5000);

来自iqdb的响应:

<error message="file_1.jpg: Not an image or image format not supported (server says it is application/octet-stream )." info="<br>Make sure the source URL points directly to the image, not to the page containing the image, and that the server does not block hotlinking."/>

这是否意味着电报阻止了热链接,或者我没有正确设置连接的Content-Type?

1 个答案:

答案 0 :(得分:0)

查询参数中的with open(dirforR , 'a') as f: writer = csv.writer(f) writer.writerow(['Subject', 'value1', 'concussion']) for row in sorted_combined: if len(row)==0: pass else: l=[val[0] for val in row.items()] for value in list([val[0] for val in row.values()][0]): l.append(value) writer.writerow(l) 似乎无效。尝试通过浏览器通过url获取图像,您将获得https://api.telegram.org/file/botAPITokenHidden/photos/file_1.jpg状态代码。您必须确保图像确实保存在电报中。