2010-03-30 49 views
0

我想根據http url返回的結果生成html內容。如何在Asp.net中使用HTTP webservice?

http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=X1-ZWz1c239bjatxn_5taq0&address=2114+Bigelow+Ave&citystatezip=Seattle%2C+WA

本頁面會給你一些XML結果。我想轉換爲使用該XML來生成HTML。我不知道從哪裏開始?有人會提供任何指南或asp.net示例代碼?

詳情:http://www.zillow.com/howto/api/GetDeepSearchResults.htm

+0

這個問題注入樣式表元素到生成的XML是「過於寬泛」的精髓:_Please編輯的問題將其限制在有足夠的細節,具體問題找出適當的答案._ – halfer 2017-11-23 23:08:39

回答

1

要取你可以使用HttpWebRequest類的數據,這是一個例子,我有手,但它可能是你需要稍微過頭了(你需要確保你做正確的事情 - 我懷疑上述是一個GET而不是POST)。

Uri baseUri = new Uri(this.RemoteServer); 

HttpWebRequest rq = (HttpWebRequest)HttpWebRequest.Create(new Uri(baseUri, action)); 
rq.Method = "POST"; 
rq.ContentType = "application/x-www-form-urlencoded"; 

rq.Accept = "text/xml"; 
rq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; 

Encoding encoding = Encoding.GetEncoding("UTF-8"); 
byte[] chars = encoding.GetBytes(body); 
rq.ContentLength = chars.Length; 

using (Stream stream = rq.GetRequestStream()) 
{ 
    stream.Write(chars, 0, chars.Length); 
    stream.Close(); 
} 

XDocument doc; 
WebResponse rs = rq.GetResponse(); 
using (Stream stream = rs.GetResponseStream()) 
{ 
    using (XmlTextReader tr = new XmlTextReader(stream)) 
    { 
     doc = XDocument.Load(tr); 
     responseXml = doc.Root; 
    } 

    if (responseXml == null) 
    { 
     throw new Exception("No response"); 
    } 
} 

return responseXml; 

一旦你把數據傳回,你需要渲染HTML,很多很多的選擇 - 如果你只是轉換你有與最小的進一步處理HTML是什麼,那麼你可以使用XSLT - 這是一個獨立的問題。如果你需要用它做什麼,那麼這個問題太模糊,你需要更具體。

0

創建一個XSL樣式表,並從格蘭頁