开发者

how to read the response from a web site?

开发者 https://www.devze.com 2023-03-10 03:39 出处:网络
I have a website url which gives corresponding city names by taking zip code as input parameter. Now I want to know how to read the resp开发者_开发百科onse from the site.

I have a website url which gives corresponding city names by taking zip code as input parameter. Now I want to know how to read the resp开发者_开发百科onse from the site.

This is the link I am using http://zipinfo.com/cgi-local/zipsrch.exe?zip=60680


You'll have to Use the HTTPWebRequest object to connect to the site and scrape the information from the response.

Look for html tags or class names that wrap the content you are trying to find, then use either regexes or string functions to get the required data.

Good example here:


try this (you'll need to include System.text and System.net)

WebClient client = new WebClient();
string url = "http://zipinfo.com/cgi-local/zipsrch.exe?zip=60680";
Byte[] requestedHTML;
requestedHTML = client.DownloadData(url);

UTF8Encoding objUTF8 = new UTF8Encoding();
string html = objUTF8.GetString(requestedHTML);
Response.Write(html);


The simplest way it to use the light-weight WebClient classes in System.Net namespace. The following example code will just download the entire response as a string:

using (WebClient wc = new WebClient())
{
   string response = wc.DownloadString("http://zipinfo.com/cgi-local/zipsrch.exe?zip=60680");
}

However, if you require more control over the response and request process then you can use the more heavy-weight HttpWebRequest Class. For instance, you may want to deal with different status codes or headers. There's an example of using HttpWebRequest this in the article How to use HttpWebRequest and HttpWebResponse in .NET on CodeProject.


Used the WebClient Class (http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=VS.100%29.aspx) to request the page and get the response as a string.

        WebClient wc = new WebClient();
        String s = wc.DownloadString(DestinationUrl);

You can search the response for specific HTML using String.IndexOf, SubString, etc, regular expressions, or try something like the HTML Agility Pack (http://htmlagilitypack.codeplex.com/) which was created specifically to help parse HTML.


first of all, you better find a good Web Service for this purpose.

and this is an HttpWebRequest example:

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://zipinfo.com/cgi-local/zipsrch.exe?zip=60680");
                httpRequest.Credentials = CredentialCache.DefaultCredentials;     
                HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

                Stream dataStream = httpResponse.GetResponseStream();


You need to use HttpWebRequest for receiving content and some tools for parsing html and finding what you need. One of the most popular libs for working with html in c# is HtmlAgilityPack, you can see simple example here: http://www.fairnet.com/post/2010/08/28/Html-screen-scraping-with-HtmlAgilityPack-Library.aspx


you can use a WebClient object, and an easy way to scrape the data is with xpath.

0

精彩评论

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