WebClient模拟⽹页提交表单
最近学习Asp开发,迷上了Http的传输,⼩试⽜⼑!
1、Web页⾯:
很简单的⼀个页⾯,功能填⼊⼀些数据,保存到服务器C盘的⼀个⽂件,效果图:
前台:
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Nankang.Test.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="/1999/xhtml">
<head runat="server">
<title>保存</title>
</head>
<body>
<form id="form1" runat="server" enableviewstate="False">
<div>
<asp:TextBox ID="m_TextBox" runat="server" Height="111px" Width="291px"></asp:TextBox>
<br />
<asp:Button ID="m_Button" runat="server" Text="保存" onclick="m_Button_Click" />
<asp:Label ID="m_Label" runat="server" Text="..."></asp:Label>
<br />
简单网页</div>
</form>
</body>
</html>
后台:
View Code
protected void m_Button_Click(object sender, EventArgs e)
{
StreamWriter sw = null;
try
{
string dir = @"C:\Test\";
if (Directory.Exists(dir) == false)
{
Directory.CreateDirectory(dir);
}
string path = dir + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
FileStream fs = new FileStream(path, FileMode.Create);
sw = new StreamWriter(fs);
sw.WriteLine(this.m_TextBox.Text);
this.m_Label.Text += "保存成功!";
}
catch (Exception ex)
{
this.m_Label.Text = ex.Message;
}
finally
{
if (sw != null)
{
sw.Close();
}
}
}
2、在WinForm中,⽤WebClient,调⽤Post⽅法,发送数据
页⾯效果图:
后台关键代码:
View Code
WebClient webClient = new WebClient();
byte[] responseData = webClient.DownloadData(uriString);
string srcString = Encoding.UTF8.GetString(responseData);
webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
//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);
//EventValidation
string eventValidationFlag = "id=\"__EVENTVALIDATION\" value=\"";
i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
j = srcString.IndexOf("\"", i);
string eventValidation = srcString.Substring(i, j - i);
//获取页⾯的章数
string viewmenuid = "<input name=\"txt_menuid\" type=\"text\" value=\"";
i = srcString.IndexOf(viewmenuid) + viewmenuid.Length;
j = srcString.IndexOf("\"", i);
string txt_menuid = srcString.Substring(i, j - i);
//提交按钮的⽂本
string submitButton = "保存";
bookTitle = System.Web.HttpUtility.UrlEncode(bookTitle);
viewState = System.Web.HttpUtility.UrlEncode(viewState);
eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
txt_menuid = System.Web.HttpUtility.UrlEncode(txt_menuid);
submitButton = System.Web.HttpUtility.UrlEncode(submitButton);
string postString = "__VIEWSTATE=" + viewState + "&__EVENTVALIDATION=" + eventValidation + "&m_TextBox=" + bookTitle + "&m_Button=" + submitButton;
// 将字符串转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
responseData = webClient.UploadData(uriString, "POST", postData);
srcString = Encoding.UTF8.GetString(responseData);
MessageBox.Show("保存成功!");
3、Code
仅供参考,谢谢。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论