Click here to Skip to main content
15,886,740 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I'm working with a mapquest App, and on the client side, i have the following Ajax Request:

C#
$.ajax({
                            url: 'http://www.mapquestapi.com/search/v1/radius',
                            dataType: 'jsonp',
                            crossDomain: true,
                            data:
                            {
                                key: decodeURI('MyKey'),
                                origin: city + ', ' + state,
                                radius: 300,
                                hostedData: 'MyData',
                                maxMatches: 4
                            },
                            success: jsonResponse
                        });


For the above call, jsonResponse function kicks in and works perfectly well.

Now, i want to make this a server side routine in my ASP.NET MVC3 Web App.

I've Written the following code to imitate client side:

C#
 requestUrl = @"http://www.mapquestapi.com/search/v1/radius";

// Create Request
var request = HttpWebRequest.Create(requestUrl);

request.Method = "POST";

// Set method type for request
request.ContentType = "application/json";

var options  = new
{

    key = HttpUtility.UrlDecode("MyKey"),
    origin = requestvm.City + "," + requestvm.State,
    radius = 300,
    hostedData = "MyData",
    maxMatches = 4
};

// Using Newtonsoft.Json
var jsonobj = JsonConvert.SerializeObject(options);
try
{
    using (StreamWriter requestWriter = new StreamWriter(request.GetRequestStream()))
    {
        requestWriter.Write(jsonobj);
    }
}
catch (System.Net.WebException ex)
{
    return null;
}

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    if (response.StatusCode != HttpStatusCode.OK)
    {
        ModelState.AddModelError("", "Request Staus was not OK. Please try again");
        return View(requestvm);
    }

    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        var serverResponse = reader.ReadToEnd();


        var responseData = JsonConvert.DeserializeObject(serverResponse);

        return null;
    }

}


So when i do a breakpoint check var responseData, i get an error message from Mapquest, saying you need to provide the Key parameter. Can anyone tell me why the Key param is not working on the server side, but it is on the client?

Any suggestions are appreciated. Thanks.
Posted
Updated 24-Jul-12 6:28am
v2

1 solution

I did manage to solve this yesterday. It was a mistake on my part. After checking help for data at JQuery.Ajax() calls, found out that data puts/appends all it's contents as Query string, and it Json serializes it if it's not already a string.



Regarding mapquest, i checked that they dont have POST requests (i'm not sure of this, have to research more as this is a much safer way to get things done).



My working code is below:

C#
                string key = HttpUtility.UrlDecode("MyKey"); 
                string hostedData = "MyHosted,MySpecificField";
                // example: "MQA.MQ_999_Sample,T=3500" 

                string radius = "300"; 
                string maxMatches = "1"; 
                string origin = 
                HttpUtility.UrlDecode(requestvm.City + "," + requestvm.State); 
 
   requestUrl = @"http://www.mapquestapi.com/search/v1/radius?key=" + key 
                                + "&hostedData=" + hostedData 
                                + "&radius=" + radius 
                                + "&maxMatches=" + maxMatches 
                                + "&origin=" + origin; 
 
                // Create Request 
                var request = HttpWebRequest.Create(requestUrl); 
 
                // set request method 
                request.Method = "GET"; 
 
                // set content type 
                request.ContentType = "application/x-www-form-urlencoded"; 
                
                // get response for GET request 
                using (HttpWebResponse response = 
                       request.GetResponse() as HttpWebResponse) 
                { 
                    if (response.StatusCode != HttpStatusCode.OK) 
                    { 
                        ModelState.AddModelError("",
                        "Request Staus was not OK. Please try again"); 
                        return View(requestvm); 
                    } 
 
                    using (StreamReader reader = 
                    new StreamReader(response.GetResponseStream())) 
                    { 
                        var serverResponse = reader.ReadToEnd(); 
 
                        RadiusResponse radiusResponse = new RadiusResponse(); 
 
                radiusResponse =  
                JsonConvert.DeserializeObject<radiusresponse>(serverResponse); 
                         
                /* at this point, radiusResponse contains the correct
                   response from server. 
                   I also modeled RadiusReponse, as POCO to mimic the 
                   response structure sent from mapquest 
                   (which i got the idea from a GoogleMap api help post). 
                   The advantage of this is that 
                   JsonConvert.Deserialize will now populate each 
                   and every RadiusReponse POCO structure, and now we
                   can use it in a typesafe way. 
                */ 
                } 
          }
</radiusresponse>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900