开发者

How to consume an HTTP webservice in Asp.net?

开发者 https://www.devze.com 2022-12-25 15:42 出处:网络
I want to ge开发者_开发技巧nerate html content based on a result returned by http url. http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=X1-ZWz1c239bjatxn_5taq0&address=2114+Bigelo

I want to ge开发者_开发技巧nerate html content based on a result returned by http url.

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

This page will give you some XML results. I want to convert to use that XML to generate HTML. I am not getting any idea where to start? Would someone offer any guidelines or sample code for asp.net?

For details: http://www.zillow.com/howto/api/GetDeepSearchResults.htm


To fetch the data you can use the HttpWebRequest class, this is an example I have to hand but it may be slightly overdone for your needs (and you need to make sure you're doing the right thing - I suspect the above to be a GET rather than a 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;

Once you've got the data back you need to render HTML, lots and lots of choices - if you just want to convert what you've got into HTML with minimal further processing then you can use XSLT - which is a question all on its own. If you need to do stuff with it then the question is too vague and you'll need to be more specific.


Create a xsl stylesheet, and inject the stylesheet element into the resulting xml from teh page

0

精彩评论

暂无评论...
验证码 换一张
取 消