使用HttpWebRequest的POST取得网页内容
2008-11-25 12:30
501 查看
这里我要做的是,根据IP来取IP所在的地区,使用网址www.ip138.com这个网站。
你打开这个网站你就会发现。它有一个文本框,让你输入一个IP,然后它会给你一个相应的IP所在地区,可是它却是通过post进行传值的。我们把www.ip138.com网页的首页打开,看它的原码,你会发现,提交的时候,它会提交二个东西出去,一个是IP,一个是action的值,IP不说了,是你输入的IP值,这个action值,我分析可能是IP或是电话的分类。它是一个定值,是2。这样我们就会明白了,显示的页面是ips.asp页面,这个页面要得到的值是。ip,action好,下面是我的程序
using System.Text;
using System.IO;
using System.Net这三个引用一定要引用上,不然会报错
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Net;
namespace GetPages
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string strId = "202.97.224.68";
string strPassword = "2";
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "ip=" + strId;
postData += ("&action=" + strPassword);
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create("http://www.ip138.com/ips.asp");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
// Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
string content = reader.ReadToEnd();
string con = content.Substring(content.IndexOf("本站主数据")+6, content.IndexOf("</li><li>参考数据一") - content.IndexOf("本站主数据")-1);
Response.Write(con.Trim());
}
}
你打开这个网站你就会发现。它有一个文本框,让你输入一个IP,然后它会给你一个相应的IP所在地区,可是它却是通过post进行传值的。我们把www.ip138.com网页的首页打开,看它的原码,你会发现,提交的时候,它会提交二个东西出去,一个是IP,一个是action的值,IP不说了,是你输入的IP值,这个action值,我分析可能是IP或是电话的分类。它是一个定值,是2。这样我们就会明白了,显示的页面是ips.asp页面,这个页面要得到的值是。ip,action好,下面是我的程序
using System.Text;
using System.IO;
using System.Net这三个引用一定要引用上,不然会报错
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Net;
namespace GetPages
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string strId = "202.97.224.68";
string strPassword = "2";
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "ip=" + strId;
postData += ("&action=" + strPassword);
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create("http://www.ip138.com/ips.asp");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
// Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
string content = reader.ReadToEnd();
string con = content.Substring(content.IndexOf("本站主数据")+6, content.IndexOf("</li><li>参考数据一") - content.IndexOf("本站主数据")-1);
Response.Write(con.Trim());
}
}
相关文章推荐
- 使用HttpWebRequest的POST取得网页内容(异步操作)2篇集合
- 使用HttpWebRequest的POST取得网页内容(异步操作)
- 使用HttpWebRequest的POST取得网页内容
- 使用HttpWebRequest的POST取得网页内容(异步操作)2篇集合
- wp7使用POST请求Asp.net网页时,HttpWebRequest的BeginGetResponse方法抛出NotSupportException异常的解决方法
- C#如何使用HttpWebRequest、HttpWebResponse模拟浏览器抓取网页内容
- HttpWebRequest 下载网页Html代码 POST方式(站内使用了form方式) System.Net.WebException (417) Expectation failed
- C#中使用HttpWebRequest用Post提交MultiPart数据
- WebRequest 创建 WebResponse 实例....取得别站网页内容。
- HttpWebRequest的使用之Get和Post的差别(C#)
- C#中使用HttpWebRequest用Post提交MultiPart数据
- 使用HttpWebRequest与HttpWebResponse抓取网页数据
- 使用httpwebrequest Post数据到网站
- .Net使用HttpWebRequest构造POST请求(带文件)_备忘
- 使用HttpWebRequest和HtmlAgilityPack抓取网页(拒绝乱码,拒绝正则表达式)
- C#获取网页内容 (WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)
- 使用HttpWebRequest发送HTTP请求,同时支持GET/POST方式提交。
- C# 后台使用HttpWebRequest发送POST请求帮助类
- 使用HttpWebRequest访问网页及获取数据
- C#的HttpWebRequest编程,支持带ViewState的网页POST请求