C#winform程序打开网页,怎么模拟对网页的操作?

wwwzjuteducn 2010-11-07 12:52:07
C#winform程序打开网页,怎么模拟对网页的操作?
我主要是想实现以下功能:
用C#winform实现一个C#程序,连接到某一个网址,比如说某高校的成绩查询网址http://www.ycjw.zjut.edu.cn/
然后在winform上的textbox中输入学号和密码之后按button确定就能连接到网页中的下一个页面(但是查询成绩的网页页面可能还需要多次点击链接)
这样就能查询到自己的成绩,然后显示在winform中的textBox中。。
这样用C#这么实现??具体详细点。。
...全文
738 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
NewUser2008 2010-11-08
  • 打赏
  • 举报
回复
Post请求,如果在登陆时就获取了Web发送的所有信息就直接请求想要的页面,进行正规则分析,如果转换的页面有附代信息,那就一步一步的请求相应页面
lijun881013 2010-11-08
  • 打赏
  • 举报
回复
而且怎么加这么多代码? 楼主问的问题 应该五六句代码就可以说明吧。。。
看代码看的我头晕。。。
lijun881013 2010-11-08
  • 打赏
  • 举报
回复
导入的mshtml是什么??
c_fans_2010 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yuha1100 的回复:]
再送上一段,Webbrower 模拟的代码, 楼主你不给我分, 我跟你急!


C# code

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ms……
[/Quote]
-----------------------------
代码比较经典,收藏了...
yuha1100 2010-11-08
  • 打赏
  • 举报
回复
再送上一段,Webbrower 模拟的代码, 楼主你不给我分, 我跟你急!


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using mshtml;
using System.Drawing;

namespace Yiwai
{
public class LJ1
{
public void load()
{
//Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
webBrowser2 = new WebBrowser();
this.webBrowser2.Navigate(this.m_BaseInfo.strWebAddress);
}
public LJ1() { }
public LJ1(WebBrowser web) { webBrowser2 = web; }
public static Image GetRegCodePic(HtmlDocument dd, String imgID)
{
HTMLDocument doc = (HTMLDocument)dd.DomDocument;
HTMLBody body = (HTMLBody)doc.body;
IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
IHTMLControlElement img;
img = (IHTMLControlElement)dd.GetElementById(imgID).DomElement;
rang.add(img);
rang.execCommand("Copy", false, null);
Image regImg = Clipboard.GetImage();
Clipboard.Clear();
return regImg;
}
public Image getCode()
{
// webBrowser2.Document.Window.Frames["topFrame"].Document.InvokeScript("changeLan", objects);
return GetRegCodePic(webBrowser2.Document.Window.Frames["topFrame"].Document, "vc");
}
public bool Login(string code)
{

webBrowser2.Document.Window.Frames["topFrame"].Document.GetElementById("id").InnerText = this.m_BaseInfo.strUserName;
webBrowser2.Document.Window.Frames["topFrame"].Document.GetElementById("password").InnerText = this.m_BaseInfo.strPassword;
webBrowser2.Document.Window.Frames["topFrame"].Document.GetElementById("code").InnerText = code;
webBrowser2.Document.Window.Frames["topFrame"].Document.InvokeScript("login");
return true;
}
public string getData()
{
// webBrowser2.Document.Window.Frames["SI2_mem_index"].Document.Window.Frames["body"].Document.Window.Frames["body_browse"].Document.GetElementById("Submit323").InvokeMember("click");
try
{
webBrowser2.Document.Window.Frames["mainFrame"].Document.InvokeScript("od_OnRefreshManually", new object[] { 0 });
return webBrowser2.Document.Window.Frames["mainFrame"].Document.GetElementById("levents").InnerHtml;
}
catch (Exception)
{

return "";
}

}
public LJBaseInfo m_BaseInfo;
public WebBrowser webBrowser2;

[StructLayout(LayoutKind.Sequential)]
public struct LJBaseInfo
{
public string bet;
public string gid;
public string gnum;
public string strWebAddress;
public string strUserName;
public string strPassword;
public string strUid;
public string strCookies;
}
public bool Create(string strWebAddress, string strUserName, string strPassword)
{
if (((strWebAddress == "") || (strUserName == "")) || (strPassword == ""))
{
return false;
}
this.m_BaseInfo = new LJBaseInfo();
this.m_BaseInfo.strPassword = strPassword;
this.m_BaseInfo.strWebAddress = strWebAddress;
this.m_BaseInfo.strUserName = strUserName;
this.m_BaseInfo.strCookies = "";
return true;
}
}
}



yuha1100 2010-11-08
  • 打赏
  • 举报
回复
第一webbrower. 可以模拟点击事情,
第二抓包!用HttpWebRequest
分析网页 提交什么参数。

建议抓包。 呵呵。

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Collections.Generic;
using System.Net;
using System.Drawing;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace SeBian_csdn_yuha521
{
/// <summary>
/// 工具函数类
/// </summary>
public class MyHttp
{
private CookieContainer myCookieContainer = new CookieContainer();

private string _EncodName = "gb2312";
public string EncodName
{
set { _EncodName = value; }
}

public MyHttp()
{
//
// TODO: 在此处添加构造函数逻辑
//
}



public void FreeCookie()
{
if (myCookieContainer != null)
{
myCookieContainer = null;
}

myCookieContainer = new CookieContainer();
}

public Image GetWebImage(string url)
{
try
{
url.Trim();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.AllowAutoRedirect = true;
req.Method = "GET";

// req.Headers.Add("Accept-Language", "zh-cn");
req.Headers.Add("Accept-Encoding", "gzip,deflate");
// req.Headers.Add("P3P", "CP=CAO PSA OUR");
//req.Headers.Add("Cookie", "LangKey=cs;");
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; CIBA)";
req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";

req.CookieContainer = myCookieContainer;// new CookieContainer();

req.KeepAlive = true;
req.Timeout = 30000;

HttpWebResponse webresponse = null;
try
{
webresponse = (HttpWebResponse)req.GetResponse();
if (webresponse != null)
{
return Image.FromStream(webresponse.GetResponseStream());
}
}
catch
{
return null;
}
finally
{
if (webresponse != null)
{
webresponse.Close();
}
if (req != null)
{
req.Abort();
}
}

return null;
}
catch
{
return null;
}
}

public string GetResponse(string url, string postData, string method, string RequestUri)
{
url.Trim();
HttpWebRequest request = null;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
}
catch
{
return "";
}
if (request == null)
return "";
request.Method = method;
request.Timeout = 333338;
request.KeepAlive = true;
request.AllowAutoRedirect = true;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";

request.Headers.Add("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.5");
//request.Headers.Add("Cookie", "LangKey=cs;");
request.CookieContainer = myCookieContainer;

try
{
if (RequestUri != null && RequestUri != "")
request.Referer = RequestUri;
else
request.Referer = request.RequestUri.ToString();
}
catch
{
}

request.ContentType = "application/x-www-form-urlencoded";

HttpWebResponse webresponse = null;
try
{
if (postData.Length > 0)
{
byte[] loginDataBytes = Encoding.Default.GetBytes(postData);
request.ContentLength = loginDataBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
}
webresponse = request.GetResponse() as HttpWebResponse;
if (webresponse != null)
{
return GetResponseHTML(webresponse);
}
}
catch
{
}
finally
{
if (webresponse != null)
{
webresponse.Close();
}
}

return "";
}
public int type = 0;
public string downName;
public string downUrl;
public string GetResponseHTML(HttpWebResponse response)
{
if (type == 1)
{
string imgName = "bt\\" + downName;
if (File.Exists(imgName + ".torrent"))
{
return "文件已经存在!";
}
Stream fileStream = null;
Stream responseStream = response.GetResponseStream();
fileStream = File.Create(imgName);
byte[] bytes = new byte[1024];
int count = 0;
do
{
count = responseStream.Read(bytes, 0, bytes.Length);
fileStream.Write(bytes, 0, count);
}
while (count > 0);

responseStream.Close();
fileStream.Close();
string dd = System.Windows.Forms.Application.StartupPath;
File.Move(imgName, imgName + ".torrent");
return dd + "\\" + imgName + ".torrent";
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
/*Stream fileStream = null;
string imgName = "b46b9e.torrent";


Stream responseStream = response.GetResponseStream();
fileStream = File.Create(imgName);
byte[] bytes = new byte[1024];
int count = 0;
do
{
count = responseStream.Read(bytes, 0, bytes.Length);
fileStream.Write(bytes, 0, count);
}
while (count > 0);
*/
string strHTML = reader.ReadToEnd();
strHTML = strHTML.Replace("\n", "");
strHTML = strHTML.Replace("\r", "");
strHTML = strHTML.Replace("\t", "");

if (reader != null)
{
reader.Close();
}

if (response != null)
{
response.Close();
}

return strHTML;
}

public string userid;
public string pwd;
}
}

wwwzjuteducn 2010-11-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 neohope 的回复:]
用get或post发送请求,分析返回内容
[/Quote]
能详细一点么? 我是新手 很多都不是很懂
neohope 2010-11-07
  • 打赏
  • 举报
回复
用get或post发送请求,分析返回内容
c_fans_2010 2010-11-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 neohope 的回复:]
用get或post发送请求,分析返回内容
[/Quote]

帮顶...

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧