简体   繁体   中英

unauthorized response from bing maps

i'm tring to get the country and region based on a point in Bing maps API, i have created a trial public web key and used it .... but it gets me the following response **

"Copyright © 2014 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation."

** when i enter the URL in my chrome browser i get the right response ... but in the debug i get that message .

that's my main method

 try
            {
                Uri locationsRequest = CreateRequest(query,key);
                Response locationsResponse = MakeRequest(locationsRequest);
                ProcessResponse(locationsResponse);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.Read();
            }

there is my url creation function

    public static Uri CreateRequest(string queryString, string BingMapsKey)
    {
        Uri UrlRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", queryString, BingMapsKey));
        return (UrlRequest);
    }

and thas my ersponse function

           public static Response MakeRequest(Uri requestUrl)
    {
        try
        {

            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                    throw new Exception(String.Format(
                    "Server error (HTTP {0}: {1}).",
                    response.StatusCode,
                    response.StatusDescription));
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
                object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                Response jsonResponse
                = objResponse as Response;
                return jsonResponse;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return null;
        }

    }

I know this is a bit late. But I encountered the same problem and the solution I found was not the code but in the data. Make sure that your queryString is a valid URL when combined with the VirtualEarth URL you are sending to Bing Maps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM