我想使用stackoverflow API的搜索方法,根據搜索關鍵字返回結果的json結構,然後在SearchResults div中顯示這些結果(標題,描述和url)。stackoverflow search api
我是新來的C#和我第一次嘗試去是這樣的:
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;' />";
}
}
問題是返回看起來像裝飾符號垃圾 - 我猜是因爲它是序列化,需要進行反序列化?
最有可能需要更改該字符串 – Marcom 2011-05-17 12:58:00
的編碼。在你的函數名一個錯字:searchStockOverflow :) – badgerr 2011-05-17 13:02:21