6

I would like to use the search method of stackoverflow API to return the json structure of results based on a search keyword and then display those results (title, description and the url) in the SearchResults div.

I am new to C# and my first attempt went something like this:

    protected void searchStockOverflow(string y)
    {

        var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.stackoverflow.com/1.1/search?intitle="+y);
        httpWebRequest.ContentType = "text/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{ \"intitle\": \"" + y + "\"}";

            streamWriter.Write(json);
        }
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();

            SearchResults.InnerHtml += "<div style='border:1px solid blue;margin:5px;'>";
            SearchResults.InnerHtml += responseText + "<br />";
            SearchResults.InnerHtml += "</div><br style='clear:both;' />";
        }
    }

The issue is that what is returned looks like dingbats rubbish - i guess because it is serialized and need to be deserialized?

2
  • most probably you need to change the encoding of the string
    – Marcom
    May 17, 2011 at 12:58
  • 1
    There is a typo in your function name: searchStockOverflow :)
    – badgerr
    May 17, 2011 at 13:02

3 Answers 3

8

I would definitely say consider using the REST client; however, to look at the issues... generally you want to deserialize the data as JSON manually, then run that data through your UI code. For example:

static void SearchStackOverflow(string y)
{
    var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.stackoverflow.com/1.1/search?intitle=" + Uri.EscapeDataString(y));
    httpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
    httpWebRequest.Method = "GET";
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    string responseText;
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        responseText = streamReader.ReadToEnd();
    }
    var result = (SearchResult)new JavaScriptSerializer().Deserialize(responseText, typeof(SearchResult));
    .... do something with result ...
}
class SearchResult
{
    public List<Question> questions { get; set; }
}
class Question
{
    public string title { get; set; }
    public int answer_count { get; set; }
}

Which uses the JavaScriptSerializer from System.Web.Extensions.dll

2
  • +1 for JSON deserialization and a other fixes (URL string escaping, GET vs POST). But I don't think that AutomaticDecompression is needed, since there is no Accept-Encoding field specified in the HTTP header (i.e. in httpWebRequest).
    – vgru
    May 17, 2011 at 13:18
  • @Groo - yes, but if I post a SO API example that doesn't use compression, I'm pretty sure my colleagues will laugh at me (considering where I work, etc). And adding the support also adds the request headers (I checked in fiddler) May 17, 2011 at 13:22
3

Also Take a look at Stacky StackApps .Net Client Library which is REST-based API that provides access to stackoverflow family of websites.

0

Unfortunately I'm on my Mac and can't run a test on your code. You might want to check the character encoding of both your page and the response stream coming back. If they don't match; it could cause the characters coming from the response stream to be rendered incorrectly, hence the rubbish you're seeing.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.