火车采集器软件交流官方论坛

 找回密码
 加入会员
搜索
火车采集器V9版免费下载火车浏览器 - 可视采集,万能群发,全自动脚本工具
查看: 7695|回复: 1

C# httpwebrequest模拟浏览器提交数据

[复制链接]
发表于 2009-10-7 10:49:23 | 显示全部楼层 |阅读模式
using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Security.Cryptography;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CookieContainer cookieContainer = new CookieContainer();

        ///////////////////////////////////////////////////
        // 1. 打开 MyLogin.aspx 页面,获得 GetVeiwState & EventValidation
        ///////////////////////////////////////////////////
        // 设置打开页面的参数
        string URI = "
http://www.***.com";
        HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
        request.Method = "GET";
        request.KeepAlive = false;

        // 接收返回的页面
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        System.IO.Stream responseStream = response.GetResponseStream();
        System.IO.StreamReader reader = new System.IO.StreamReader(responseStream,Encoding.Default);
        string srcString = reader.ReadToEnd();
        //Response.Write(srcString + "<br>");
        // 获取页面的 VeiwState
        string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
        int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
        int j = srcString.IndexOf("\"", i);
        string viewState = srcString.Substring(i, j - i);
        Response.Write(viewState + "<br>");
        string submitButton = "登录";
        // 用户名和密码
        string userName = "admin";
        string password = "admin";
        // 将文本转换成 URL 编码字符串
        viewState = System.Web.HttpUtility.UrlEncode(viewState);
        eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
        submitButton = System.Web.HttpUtility.UrlEncode(submitButton);
        // 要提交的字符串数据。格式形如:user=uesr1&password=123
        string formatString = "userid={0}&password={1}&loginButton={2}&__VIEWSTATE={3}&__EVENTVALIDATION={4}";
        string postString = string.Format(formatString, userName, password, submitButton, viewState, eventValidation);
        Response.Write(postString);
        // 将提交的字符串数据转换成字节数组
        byte[] postData = Encoding.ASCII.GetBytes(postString);
        // 设置提交的相关参数
        request = WebRequest.Create(URI) as HttpWebRequest;
        request.Method = "POST";
        request.KeepAlive = false;
        request.ContentType = "application/x-www-form-urlencoded";
        request.CookieContainer = cookieContainer;
        request.ContentLength = postData.Length;
        // 提交请求数据
        System.IO.Stream outputStream = request.GetRequestStream();
        outputStream.Write(postData, 0, postData.Length);
        outputStream.Close();
        // 接收返回的页面
        response = request.GetResponse() as HttpWebResponse;
        responseStream = response.GetResponseStream();
        reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("GB2312"));
        srcString = reader.ReadToEnd();
        Response.Write("<br>" + srcString);
        foreach (Cookie cookie in response.Cookies)
            cookieContainer.Add(cookie);
    }
}
发表于 2009-10-7 15:25:56 | 显示全部楼层
{:4_191:} 有点高深!
您需要登录后才可以回帖 登录 | 加入会员

本版积分规则

QQ|手机版|Archiver|火车采集器官方站 ( 皖ICP备06000549 )

GMT+8, 2024-12-23 03:20

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表