No timeout or nothing. I tried both subscription keys. Works fine in Fiddler. Net 4.72
// The user's search string.
private static string searchString = "hummingbird";
// To page through the images, you'll need the next offset that Bing returns.
private static long _nextOffset = 0;
// To get additional insights about the image, you'll need the image's
// insights token (see Visual Search API).
private static string _insightsToken = null;
// Bing uses the X-MSEdge-ClientID header to provide users with consistent
// behavior across Bing API calls. See the reference documentation
// for usage.
private static string _clientIdHeader = null;
private const string QUERY_PARAMETER = "?q="; // Required
private const string MKT_PARAMETER = "&mkt="; // Strongly suggested
private const string COUNT_PARAMETER = "&count=";
private const string OFFSET_PARAMETER = "&offset=";
private const string ID_PARAMETER = "&id=";
private const string SAFE_SEARCH_PARAMETER = "&safeSearch=";
private const string ASPECT_PARAMETER = "&aspect=";
private const string COLOR_PARAMETER = "&color=";
private const string FRESHNESS_PARAMETER = "&freshness=";
private const string HEIGHT_PARAMETER = "&height=";
private const string WIDTH_PARAMETER = "&width=";
private const string IMAGE_CONTENT_PARAMETER = "&imageContent=";
private const string IMAGE_TYPE_PARAMETER = "&imageType=";
private const string LICENSE_PARAMETER = "&license=";
private const string MAX_FILE_SIZE_PARAMETER = "&maxFileSize=";
private const string MIN_FILE_SIZE_PARAMETER = "&minFileSize=";
private const string MAX_HEIGHT_PARAMETER = "&maxHeight=";
private const string MIN_HEIGHT_PARAMETER = "&minHeight=";
private const string MAX_WIDTH_PARAMETER = "&maxWidth=";
private const string MIN_WIDTH_PARAMETER = "&minWidth=";
private const string SIZE_PARAMETER = "&size=";
public frmTest()
{
InitializeComponent();
}
private void frmTest_Load(object sender, EventArgs e)
{
}
private async Task RunAsync()
{
try
{
// Remember to encode the q query parameter.
var queryString = QUERY_PARAMETER + Uri.EscapeDataString(searchString);
queryString += MKT_PARAMETER + "en-us";
HttpResponseMessage response = await MakeRequestAsync(queryString);
_clientIdHeader = response.Headers.GetValues("X-MSEdge-ClientID").FirstOrDefault();
// This example uses dictionaries instead of objects to access the response data.
var contentString = await response.Content.ReadAsStringAsync();
Dictionary<string, object> searchResponse = JsonConvert.DeserializeObject<Dictionary<string, object>>(contentString);
if (response.IsSuccessStatusCode)
{
ShowImages(searchResponse);
}
else
{
PrintErrors(response.Headers, searchResponse);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("\nPress ENTER to exit...");
Console.ReadLine();
}
private void ShowImages(Dictionary<string, object> response)
{
Console.WriteLine("The response contains the following images:\n");
// This example prints the first page of images but if you want to page
// through the images, you need to capture the next offset that Bing returns.
_nextOffset = (long)response["nextOffset"];
var images = response["value"] as Newtonsoft.Json.Linq.JToken;
foreach (Newtonsoft.Json.Linq.JToken image in images)
{
Console.WriteLine("Thumbnail: " + image["thumbnailUrl"]);
Console.WriteLine("Thumbnail size: {0} (w) x {1} (h) ", image["thumbnail"]["width"], image["thumbnail"]["height"]);
Console.WriteLine("Original image: " + image["contentUrl"]);
Console.WriteLine("Original image size: {0} (w) x {1} (h) ", image["width"], image["height"]);
Console.WriteLine("Host: {0} ({1})", image["hostPageDomainFriendlyName"], image["hostPageDisplayUrl"]);
Console.WriteLine();
// If you want to get additional insights about the image, capture the
// image token that you use when calling Visual Search API.
_insightsToken = (string)image["imageInsightsToken"];
}
}
private async Task<HttpResponseMessage> MakeRequestAsync(string queryString)
{
var client = new HttpClient();
// Request headers. The subscription key is the only required header but you should
// include User-Agent (especially for mobile), X-MSEdge-ClientID, X-Search-Location
// and X-MSEdge-ClientIP (especially for local aware queries).
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _subscriptionKey);
// hangs here:
return (await client.GetAsync(_baseUri + queryString));
}
private void PrintErrors(HttpResponseHeaders headers, Dictionary<String, object> response)
{
Console.WriteLine("The response contains the following errors:\n");
object value;
if (response.TryGetValue("error", out value)) // typically 401, 403
{
PrintError(response["error"] as Newtonsoft.Json.Linq.JToken);
}
else if (response.TryGetValue("errors", out value))
{
// Bing API error
foreach (Newtonsoft.Json.Linq.JToken error in response["errors"] as Newtonsoft.Json.Linq.JToken)
{
PrintError(error);
}
// Included only when HTTP status code is 400; not included with 401 or 403.
IEnumerable<string> headerValues;
if (headers.TryGetValues("BingAPIs-TraceId", out headerValues))
{
Console.WriteLine("\nTrace ID: " + headerValues.FirstOrDefault());
}
}
}
private void PrintError(Newtonsoft.Json.Linq.JToken error)
{
string value = null;
Console.WriteLine("Code: " + error["code"]);
Console.WriteLine("Message: " + error["message"]);
if ((value = (string)error["parameter"]) != null)
{
Console.WriteLine("Parameter: " + value);
}
if ((value = (string)error["value"]) != null)
{
Console.WriteLine("Value: " + value);
}
}
private void cmdSearch_Click(object sender, EventArgs e)
{
RunAsync().Wait();
}
}