IAmMakingAThing IAmMakingAThing - 1 month ago 25
ASP.NET (C#) Question

Can't get Bing Image Search Api Version 5 to return images

I hope someone can help me. I am trying to get the Bing Version 5 Image Search Api to return some actual results but it doesn't seem to want to :(.

When I use the testing console in the below link, it returns images just fine:
https://dev.cognitive.microsoft.com/docs/services/56b43f0ccf5ff8098cef3808/operations/571fab09dbe2d933e891028f/console

At this point, I've almost copied and pasted the C# code from this link:
https://dev.cognitive.microsoft.com/docs/services/56b43f0ccf5ff8098cef3808/operations/56b4433fcf5ff8098cef380c

So what I've ended up with is this, minus that I've removed my new version 5 key:

async void MakeRequest()
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);

// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "My Key goes here");

// Request parameters
queryString["q"] = "cats";
queryString["count"] = "10";
queryString["offset"] = "0";
queryString["mkt"] = "en-us";
queryString["safeSearch"] = "Moderate";
var uri = "https://api.cognitive.microsoft.com/bing/v5.0/images/search?" + queryString;

var response = await client.GetAsync(uri);

litTest.Text = response.ToString();

}


Which gives this result (sorry if it is a bit messy, I'm not used to this yet, any tips are much appreciated):

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Pragma: no-cache Vary: Accept-Encoding Cache-Control: no-store, must-revalidate, no-cache P3P: CP="NON UNI COM NAV STA LOC CURa DEVa PSAa PSDa OUR IND" Set-Cookie: SRCHD=AF=NOFORM; domain=.bingapis.com; expires=Mon, 15-Oct-2018 19:44:57 GMT; path=/ Set-Cookie: SRCHUID=V=2&GUID=4259560ECDB441FAAF5D7E390BBCF796; expires=Mon, 15-Oct-2018 19:44:57 GMT; path=/ Set-Cookie: SRCHUSR=DOB=20161015; domain=.bingapis.com; expires=Mon, 15-Oct-2018 19:44:57 GMT; path=/ Set-Cookie: _SS=SID=1992B4120F0069473316BDBD0EE76838; domain=.bingapis.com; path=/ Set-Cookie: _EDGE_S=mkt=en-us&F=1&SID=1992B4120F0069473316BDBD0EE76838; path=/; httponly; domain=bingapis.com Set-Cookie: _EDGE_V=1; path=/; httponly; expires=Mon, 15-Oct-2018 19:44:57 GMT; domain=bingapis.com Set-Cookie: MUID=14FADE76CCDC68020A5DD7D9CD3B69D8; path=/; expires=Mon, 15-Oct-2018 19:44:57 GMT; domain=bingapis.com Set-Cookie: MUIDB=14FADE76CCDC68020A5DD7D9CD3B69D8; path=/; httponly; expires=Mon, 15-Oct-2018 19:44:57 GMT BingAPIs-TraceId: 14F0C29C68D24DAA8E845774043CB487 X-MSEdge-ClientID: 14FADE76CCDC68020A5DD7D9CD3B69D8 X-MSAPI-UserState: e7cc X-MSEdge-Ref: Ref A: 14F0C29C68D24DAA8E845774043CB487 Ref B: 6ABDFD5BE70A83784C9179474D02662D Ref C: Sat Oct 15 12:44:57 2016 PST apim-request-id: 4d782700-4993-4975-9a9b-e19dea659101 Date: Sat, 15 Oct 2016 19:44:57 GMT Content-Length: 82014 Content-Type: application/json; charset=utf-8 Expires: -1 }

Can anyone help with what I'm doing wrong, I got version 2 working just fine but that's being killed off in December.

Thank you very much for reading and taking a look :).

Answer

You are not reading content of your response in your example. You need to get actual json from response's content, for example like this:

var response = await client.GetAsync(uri);
litTest.Text = await response.Content.ReadAsStringAsync();
Comments