• 全部
...

请教!winform如何传数据给网页?

ChrisNick 2005-02-20 01:02:36
大家好,我想写一个程序,功能是在一个winform里面有一个button的按钮,然后当我点击这个button,将用户名123,密码123。传给一张网页index.asp,然后要让这张网页能够接受到我用winform传过来的两个数据,并且自动运行index.asp网页中的一个button。
如果index.asp这张网页里面的代码我都不知道,能不能实现上述的要求?
又如果index.asp这张网页里:用户名的textbox的id=uid,密码的textbox的id=upwd,按钮button的id=btn。请问,我又能不能实现上面的要求呢?
我主要是想实现自动登录一张网页的作用,但是要用winform来完成。大家帮帮忙,这段代码该怎么写呢?谢谢大家。
...全文
给本帖投票
826 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
isaacyh 2005-02-20
  • 打赏
  • 举报
回复
这个我做过,请看
http://blog.csdn.net/isaacyh/archive/2005/01/27/269987.aspx
SCI-MonkeyKing 2005-02-20
  • 打赏
  • 举报
回复
学习
hedonister 2005-02-20
  • 打赏
  • 举报
回复
WebBrowser控件
ChrisNick 2005-02-20
  • 打赏
  • 举报
回复
恩,明白了。谢谢。可以结贴了。
isaacyh 2005-02-20
  • 打赏
  • 举报
回复
呵呵,网页部分的代码没有写,就是一个aspx的页面,先拖三个textbox上去,再拖一个button上去(都是web的,不是html的),
三个textbox依次命名为:TextBoxFileName,TextBoxUser,TextBoxPassword,
button命名为:ButtonForData,处理方法就写在这个buttonclick方法里,可以直接取this.TextBoxFileName等控件的值即可以在buttonclick里写
"string ss = this.TextBoxFileName.Text;"

request1.ContentType = "application/x-www-form-urlencoded";
这里是直接用sniffer截获IP包看到的,所有的发到IIS的都有这东西,就放在这里了。。。。它是由IIS去解释的。。。


得到页面数据段就是重组一个http包,但为了不去了解协议就直接将收到的包替换一些,再发到IIS让它去解析。。。

这里的替换就是加入了"&TextBoxFileName="你要传的值;等

其他的类似,至于button的,就是那句&ButtonForData=Message;
它表示让IIS知道你点击了这个button。。。

这样IIS(就是webserver)就会去执行你buttonclick方法了。。。

flame_qin 2005-02-20
  • 打赏
  • 举报
回复
remoting可以实现
ChrisNick 2005-02-20
  • 打赏
  • 举报
回复
先谢谢isaacyh(发现自己啥都不懂。。。) 朋友给我的这个连接。但是里面有写不明白,能不能说明一下。代码在这里:
public class FtpMessage
{
private string m_fileName;
private string m_host;
private string aspValue;

public FtpMessage(string fileName,string hostUrl)
{
//指定的一个信息,将用于填写TextBoxFileName。
m_fileName = fileName;
//指定的URL
m_host = hostUrl;
}


public bool SendCompleteMessage(string user,string password)
{
bool isSendMessageSuccess = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://"+m_host);
//得到网页。
WebResponse response = null;
response = request.GetResponse();
Stream readStream = response.GetResponseStream();
StreamReader sr = new StreamReader(readStream,Encoding.GetEncoding("utf-8"));
string content = "";
int index = -1;
//寻找数据
while (index < 0 && content != null)
{
content = sr.ReadLine();
//得到标准的ASPX页面的头一共26个字节,以("name=\"__VIEWSTATE\" value=\""开始的
index = content.IndexOf("name=\"__VIEWSTATE\" value=\"",0);
}
index += 26;
int endIndex = content.LastIndexOf("\"");
//形成目标数据。
if (index > 26 && endIndex > index)
{
//得到页面数据段
aspValue = content.Substring(index,endIndex-index);
StringBuilder tempData = new StringBuilder();
tempData.Append("__VIEWSTATE=");
tempData.Append(HttpUtility.UrlEncode(aspValue));
//填写TextBoxFileName的值,其值见后
tempData.Append("&TextBoxFileName=");
tempData.Append("(content1)");
//填写TextBoxUser的值,其值见后
tempData.Append("&TextBoxUser=");
tempData.Append("(content2)");
//填写TextBoxPassword的值,其值见后
tempData.Append("&TextBoxPassword=");
tempData.Append("(content3)");
//填写Button Click 的Message
tempData.Append("&ButtonForData=");
tempData.Append(HttpUtility.UrlEncode("Message"));
aspValue = tempData.ToString();
}

string content1 = m_fileName;
string content2 = user;
string content3 = password;
WebResponse response1 = null;
//替换预传送的数据。
string tempData1 = aspValue.Replace("(content1)",HttpUtility.UrlEncode(content1));
string tempData2 = tempData1.Replace("(content2)",HttpUtility.UrlEncode(content2));
string tempData3 = tempData2.Replace("(content3)",HttpUtility.UrlEncode(content3));
byte [] postData = Encoding.UTF8.GetBytes(tempData3.ToString());

HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("http://"+m_host);

//设定工作属性。
request1.Method = "POST";
request1.ContentType = "application/x-www-form-urlencoded";
request1.ContentLength = postData.Length;

//上传数据。
Stream writeStream = request1.GetRequestStream();
writeStream.Write(postData,0,postData.Length);
writeStream.Close();

//下载回应消息。
string serverMessage = "";
try
{
response1 = request1.GetResponse();
//这里的response1是Server在Button点击后跳转到的另一个页面,这个页面有一个值表示是否成功
//我将取得其值作为函数的返回值
Stream readStream1 = response1.GetResponseStream();
int i = 1024;
byte[] hehe = new byte[i];
readStream1.Read(hehe,0,i);
readStream1.Close();


StringBuilder hehe1 = new StringBuilder();
//由于我知道页面的结构,我可以从页面得到这个值。
for(int j=658;j<662;j++)
{
hehe1.Append((char)hehe[j]);
}
serverMessage = hehe1.ToString();

}
catch(Exception E)
{
string tempError = E.Message;
}
if(serverMessage == "true")
{
isSendMessageSuccess = true;
}
return isSendMessageSuccess;
}
}



这里面的代码这几句是什么意思:
request1.ContentType = "application/x-www-form-urlencoded";

//得到页面数据段
aspValue = content.Substring(index,endIndex-index);
StringBuilder tempData = new StringBuilder();
tempData.Append("__VIEWSTATE=");
tempData.Append(HttpUtility.UrlEncode(aspValue));
//填写TextBoxFileName的值,其值见后
tempData.Append("&TextBoxFileName=");
tempData.Append("(content1)");
//填写TextBoxUser的值,其值见后
tempData.Append("&TextBoxUser=");
tempData.Append("(content2)");
//填写TextBoxPassword的值,其值见后
tempData.Append("&TextBoxPassword=");
tempData.Append("(content3)");
//填写Button Click 的Message
tempData.Append("&ButtonForData=");
tempData.Append(HttpUtility.UrlEncode("Message"));
aspValue = tempData.ToString();

在这里面用户名和密码还有button的id是写在哪里的。
ChrisNick 2005-02-20
  • 打赏
  • 举报
回复
谢谢大家的意见,我先看看,多学习。如有更好的方法请留言。
wj2929 2005-02-20
  • 打赏
  • 举报
回复
winform跟网页由什么关系吗?
如果通过WebBrowser控件打开网页实现起来很简单的
yufenfeila 2005-02-20
  • 打赏
  • 举报
回复
首先确定index.asp中,点击button后会提交到哪里

就是<form action="???"
???就是要提交的页面

然后如果用GET的方法,你直接访问 ???.aspx?uid="xxx"&upwd="xxx"
就可以登录了

111,080

社区成员

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

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

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

手机看
关注公众号

关注公众号

客服 返回
顶部