HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://flash.163.com/"); //向网络发出请求
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //返回网络响应
Stream resStream = response.GetResponseStream();//获取数据流
StreamReader sr = new StreamReader(resStream, Encoding.Default);//以默认编码读取数据
strtext= sr.ReadToEnd();
sr.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //返回网络响应
Stream resStream = response.GetResponseStream();//获取数据流
StreamReader sr = new StreamReader(resStream, Encoding.Default);//以默认编码读取数据
strtext= sr.ReadToEnd();
sr.Close();
我来凑热闹。:)
如果url是这样的呢?
http://www.zjquik.com/xtnew/time/hztime.htm
好像用你的方法是不行的。
1:htm类型的,没有<form></form>得页面如何获取源代码?
2:如果该网页只接收中文参数,当使用HttpWebRequest变量时,中文参数会自动在uri中进行Base64编码。这样,目标网页就没有接收到正确的带参地址
把中文参数连接成字符串后设置HttpWebRequest的ContentLength属性为字符串长度;再设置ContentType为"application/x-www-form-urlencoded";再将字符串用Headers.Add(字符串)添加到Header里去,后面的步骤就不说了。
把中文参数联接成字符串---是不是说把上书的作为一个字符串,不放在url后面,不经过任何编码,但是用header.add方法加上取
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;namespace test
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(880, 560);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(904, 581);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
string strValue = "userlx=java&username=你在java-cn.com的用户名&password=你在java-cn.com的密码&cookies=30&login=登录"; //连接参数,采用Post方式传递 textBox1.Text = GetWeb("http://www.java-cn.com/pub/checklogin.jsp?where=login",true,strValue); } private string GetWeb(string url,bool isPost,string strValue)
{
HttpWebRequest request; StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
int i=0, j;
while(i<strValue.Length)
{
j=strValue.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(strValue.Substring(i, strValue.Length-i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(strValue.Substring(i, j-i)));
UrlEncoded.Append(strValue.Substring(j,1));
i = j+1;
}
//对参数编码:UrlEncode if (isPost)
{
//POST方式提交表单,要设置Method、ContentType、ContentLength
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
byte[] SomeBytes = Encoding.GetEncoding("gb2312").GetBytes(UrlEncoded.ToString());
request.ContentLength = SomeBytes.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);//把参数用流对象写入request对象中
newStream.Close();
}
else
{
//GET方式提交表单
request = (HttpWebRequest)WebRequest.Create(url + "?" + UrlEncoded.ToString());
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();//获得服务器响应对象
Stream resStream = response.GetResponseStream();//转成流对象
StreamReader sr = new StreamReader(resStream, Encoding.GetEncoding("gb2312")); string strtext = "";
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
while (count > 0)
{
String str = new String(read, 0, count);
strtext += str;
count = sr.Read(read, 0, 256);
}//读取内容
sr.Close(); return strtext;
}
}
}
对不同网站,有些是要utf-8编码,有些是要gb2312编码,有些网站的参数编码方式也不同一般,你要根据各种情况分析,比较复杂的说:)
这还只是前期工作,后面取得内容后的分析更繁重,而且很难找到什么好办法,郁闷吧,呵呵。
楼主,我明白了,你明白了没有?
多谢blackcatiii(能教我做框架设计吗)
to lansluo(最后一个女巫)
俺终于明白辣:)