我需要务实地使用Google反向图片搜索,问题是他们弃用了API的版本 允许用图像搜索图像。在我的场景中,我无法通过文字真正使用搜索图像。
所以我决定做一个"快速"原型通过progragmatically将图像发布到http://images.google.com/searchbyimage/upload 首先我手动完成并用提琴手嗅闻,看看发布了什么,I have also found this useful stackoverflow post.(mikerobi' s)
长话短说我需要将其发布到http://images.google.com/searchbyimage/upload的原因是因为我需要检索tbs:sbi = xxx 邮寄归来;这个"图像指纹/编码图像" (不是base64编码图像)是后期帖子的参数 需要完成以检索图像结果(再次以编程方式)
以progragmatically方式发布的参数几乎与使用浏览器时完成的参数相同 我的示例代码如下; 结果确实给了我一个tbs:sbi = xxx但它不是正确的,我在做什么 错吗
这是我的代码示例
[TestMethod]
public void PostImageToGoogleAsBytes()
{
const string googleImageSearchUrl = @"http://images.google.com/searchbyimage/upload. ";
const string imageToUpload = @"C:\Development\ReverseImageSearch\Tests\TestFiles\Images\sampleImage.jpg";
HttpContent bytesContent = new ByteArrayContent(File.ReadAllBytes(imageToUpload));
bytesContent.Headers.Add("Content-Type","image/jpeg");
bytesContent.Headers.Add("filename", "sampleImage.jpg");
using (var client = new HttpClient())
{
using (var formData = new MultipartFormDataContent())
{
formData.Add(new StringContent(String.Empty),"image_url");
formData.Add(bytesContent, "encoded_image");
formData.Add(new StringContent(String.Empty), "fileName");
formData.Add(new StringContent(String.Empty), "image_content");
var response = client.PostAsync(googleImageSearchUrl, formData).Result;
Assert.IsTrue(response.IsSuccessStatusCode);
}
}
}