MicrosoftDocs / azure-docs

Open source documentation of Microsoft Azure
https://docs.microsoft.com/azure
Creative Commons Attribution 4.0 International
10.21k stars 21.36k forks source link

Uri format #19101

Closed cbrit closed 5 years ago

cbrit commented 5 years ago

When I run the program I get the following exception:

Unhandled Exception: System.UriFormatException: Invalid URI: The format of the URI could not be determined.

Here is my code for reference

using System;
using System.Net;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace GetImage
{
    class Program
    {
        const string key = "<key>";
        const string uri = "https://api.cognitive.microsoft.com/bing/v7.0/images/search";
        const string query = "test";

        // Define result type
        struct SearchResult
        {
            public String jsonResult;
            public Dictionary<String, String> relevantHeaders;
        }

        static SearchResult BingImageSearch(string query)
        {
            var uriQuery = uri + "?q=" + Uri.EscapeDataString(query);
            WebRequest request = WebRequest.Create(query);
            request.Headers["Ocp-Apim-Subscription-Key"] = key;
            HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result;
            string json = new StreamReader(response.GetResponseStream()).ReadToEnd();

            // Create result object for response values
            var searchResult = new SearchResult()
            {
                jsonResult = json,
                relevantHeaders = new Dictionary<string, string>()
            };

            // Extract headers
            foreach (String header in response.Headers)
            {
                if (header.StartsWith("BingAPIs-") || header.StartsWith("X-MSEdge-"))
                    searchResult.relevantHeaders[header] = response.Headers[header];
            }

            return searchResult;
        }
        static void Main()
        {
            SearchResult result = BingImageSearch("test");
            dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result.jsonResult);

            var firstJsonObj = jsonObj["value"][0];
            Console.WriteLine("Title for the first image: " + firstJsonObj["name"] + "\n");
            Console.WriteLine("URL for the first image result: " + firstJsonObj["webSearchUrl"] + "\n");
        }
    }
}

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Alberto-Vega commented 5 years ago

@cbrit Sorry to hear that you are facing issues. How is that invalid URI looking? can you please print to the console and share it?

cbrit commented 5 years ago

Thanks @Alberto-Vega-MSFT I found my issue..

WebRequest request = WebRequest.Create(query);

Should be

WebRequest request = WebRequest.Create(uriQuery);

I should have read my code more closely! Sorry!

YutongTie-MSFT commented 5 years ago

@cbrit We will now proceed to close this thread. If there are further questions regarding this matter, please respond here and @YutongTie-MSFT and we will gladly continue the discussion.