daixinet

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

    最近学习Asp.net开发,迷上了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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/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

源码下载

仅供参考,谢谢。

posted on 2013-01-16 12:14  daixinet.com  阅读(3721)  评论(0编辑  收藏  举报