Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stackoverflow search api

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?

like image 515
khuzbuzz Avatar asked May 17 '11 12:05

khuzbuzz


People also ask

Is there a Stackoverflow API?

I did what any good developer would do and did a quick google for "stackoverflow api" and ended up at the Stack Exchange API. As I expected, there's an API for pretty much every aspect of the site. Even better, you can do things without authentication or an API key.

What is a stackoverflow search?

Stack Overflow is a question and answer website for professional and enthusiast programmers. It is the flagship site of the Stack Exchange Network. It was created in 2008 by Jeff Atwood and Joel Spolsky. It features questions and answers on a wide range of topics in computer programming.

Does Stackoverflow use Elasticsearch?

Our site search is powered by Elasticsearch. Our Elasticsearch cluster contains one index per site on the Stack Exchange network: stackoverflow.com has an index, superuser.com has an index, bicycles.stackexchange.com has an index, you get the idea.


2 Answers

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

like image 194
Marc Gravell Avatar answered Sep 21 '22 10:09

Marc Gravell


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

like image 40
Alireza Maddah Avatar answered Sep 21 '22 10:09

Alireza Maddah