none
How to add URL Parameters / HTTP GET / REST Request RRS feed

  • Question

  • Hi,

    I am implementing a REST service call in C#. Net using HTTP Get method.

    In the code below; I am adding parameters to the headers so they can be sent along with the request but the response is not correct.

    When I modify the URI address and append parameters to it as below, I get the correct response.

    www.abc.com/search?word=WORD&SDate=2014-09-05&EDate=2015-03-05&token=TOKEN

    Trying to make sure and learn;

    1. How to add URL parameters via .Net code (similar to how I did below to add parameters in Header of the request)?

    2. Adding parameters to the URI is correct way?

    3. Any other recommendation or suggestion

    Uri MoodysWebAddress = new Uri(www.abc.com/search) // Create the web request HttpWebRequest request = WebRequest.Create(MoodysWebAddress) as HttpWebRequest; //Set type to GET request.Method = "GET"; request.ContentType = "text/xml"; string results = string.Empty; //Create the request to send string word = "any word" string SDate = "2014-09-05"; string EDate = "2015-03-05"; string token = "TOKEN" //search word request.Headers.Add("word ", HttpUtility.UrlEncode(word )); //Start Date request.Headers.Add("SDate", HttpUtility.UrlEncode(SDate )); //End Date request.Headers.Add("EDate", HttpUtility.UrlEncode(EDate )); //token request.Headers.Add("token", HttpUtility.UrlEncode(token)); //Get response HttpWebResponse response; using (response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); results = reader.ReadToEnd(); }



    Thanks



    • Edited by KBRJ Tuesday, May 26, 2015 7:06 PM
    Tuesday, May 26, 2015 6:59 PM

Answers

  • Hi KBRJ,

    Yes, we can add parameter to the Uri, if you want to add URL parameters via .Net code, I will recommend you add the URL parameters into the Uri string directly, for more information, please try to refer to the following code:

            string word = "any word";
            string SDate = "2014-09-05";
            string EDate = "2015-03-05";
            string token = "TOKEN";
            string MoodysWebstring = @"http://www.abc.com/search?word=" + word + "&SDate=" + SDate + "&EDate=" + EDate + "&token=" + token;
            Uri MoodysWebAddress = new Uri(MoodysWebstring);
            HttpWebRequest request = WebRequest.Create(MoodysWebAddress) as HttpWebRequest;  
            request.Method = "GET";
            request.ContentType = "text/xml";
            string results = string.Empty;
            HttpWebResponse response;
            using (response =  request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                results = reader.ReadToEnd();
            }

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Wednesday, May 27, 2015 4:48 AM
    Moderator