开发者

ASP.NET Agility Pack | How to parse a web page. that is taking post parameters?

I want to parse and page that takes POST parameters. like this is my scenario. i have to parse some search results. but the search parameter are sent in post body to that page.

To parse the search result i have to send parameters to that 开发者_JAVA百科page in POST. how i can do that with agility pack ?

please help me.


Yes, It is possible, you can post parameters using HTML Agility Pack. Have a look on below code.

BrowserSession b = new BrowserSession();
b.Get("http://www.skyline-eng.com/");
b.FormElements["navigationTypeID"] = rblCategory.SelectedItem.Value;
b.FormElements["navigationSeriesID"] = boxItem.Value;
HtmlDocument docSkyLine = b.Post("http://www.skyline-eng.com/");

Here navigationTypeID and navigationSeriesID are the post parameters. Use like this and keep parsing data using the great tool HTMLAgility Pack.


Use a WebClient to make the post sending what ever parameters the search page needs. The nusing Html Agility pack you can parse the returned html.

The WebClient will have the html that was returned by the search page result.

Something like this:

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://exposureroom.com");
  request.Method = "POST";
  request.ContentType = "application/x-www-form-urlencoded";
  string formParameters = "name1=value1&name1=value2";
  byte[] requestBuffer = Encoding.ASCII.GetBytes(formParameters);
  var requestStream = request.GetRequestStream();
  requestStream.Write(requestBuffer, 0, requestBuffer.Length);
  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  StreamReader reader = new StreamReader(response.GetResponseStream());
  string str = reader.ReadToEnd();

In your case, the string variable formparameters need to contain the query parameters your search page requires. These parameters will then be sent to your search page in the form of a "POST" Http method. Of course the url should be changed to your url as well.


I don't think this is what HTML agility pack is for. It's not HTTP agility pack, it doesn't parse any HTTP request. It just parses HTML output (I.e. responses, not requests).

You can access POST parameters received by a page using the Page.Request[<param>] dictionary in the code behind of that page.

If that's not what you're trying to do can you clarify a bit?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜