怎么写客户端显示服务器上rg文件中数据
怎么写客户端客户端要现实服务器上rg文件中的数据
2012-11-15 15:45
2012-11-15 17:18
2012-11-19 13:01
程序代码:
using System;
using using using using System.Text;
using System.Windows.Forms;
namespace ReadFileFromServer
{
public partial class FormMain : Form
{
#region 全局字段
private string _tempFile;
#endregion
#region 构造函数
public FormMain()
{
InitializeComponent();
RbAnonymous.CheckedChanged += RbAnonymous_CheckedChanged;
}
#endregion
#region 控件事件
private void ButGet_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(TbUri.Text)) return;
var uri = new Uri(TbUri.Text);
if (RbAnonymous.Checked)
{
if (RbGetData.Checked) GetDataWithAnonymous(uri);
else GetFileWithAnonymous(uri);
}
else
GetWithWebService(uri);
}
void RbAnonymous_CheckedChanged(object sender, EventArgs e)
{
GbGetMode.Enabled = RbAnonymous.Checked;
}
#endregion
#region 事件处理
void LzsdNet_GetFileCompleted(object sender, net.lzsd.www.GetFileCompletedEventArgs e)
{
TbResult.Text = Encoding.UTF8.GetString(e.Result);
ButGet.Enabled = true;
}
void WebClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
var bytes = e.Result;
TbResult.Text = Encoding.UTF8.GetString(bytes);
ButGet.Enabled = true;
}
void WebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
ButGet.Enabled = true;
TbResult.Text = File.ReadAllText(_tempFile);
File.Delete(_tempFile);
}
#endregion
#region 私有方法
private void GetDataWithAnonymous(Uri uri)
{
var wc = new WebClient();
wc.DownloadDataCompleted += WebClient_DownloadDataCompleted;
ButGet.Enabled = false;
wc.DownloadDataAsync(uri);
}
private void GetFileWithAnonymous(Uri uri)
{
var wc = new WebClient();
wc.DownloadFileCompleted += WebClient_DownloadFileCompleted;
_tempFile = Path.GetTempFileName();
ButGet.Enabled = false;
wc.DownloadFileAsync(uri, _tempFile);
}
private void GetWithWebService(Uri uri)
{
var lzsdNet = new net.lzsd.www.WSGetFile();
lzsdNet.GetFileCompleted += LzsdNet_GetFileCompleted;
lzsdNet.GetFileAsync(uri.Segments[uri.Segments.Length - 1]);
}
#endregion
}
}
程序代码:namespace ReadFileFromServer
{
partial class FormMain
{
private components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.TbUri = new System.Windows.Forms.TextBox();
this.ButGet = new System.Windows.Forms.Button();
this.TbResult = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.RbAnonymous = new System.Windows.Forms.RadioButton();
this.RbWebService = new System.Windows.Forms.RadioButton();
this.GbGetMode = new System.Windows.Forms.GroupBox();
this.RbGetFile = new System.Windows.Forms.RadioButton();
this.RbGetData = new System.Windows.Forms.RadioButton();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.GbGetMode.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
this.TbUri.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
this.TbUri.Location = new System.Drawing.Point(43, 12);
this.TbUri.Name = "TbUri";
this.TbUri.Size = new System.Drawing.Size(211, 21);
this.TbUri.TabIndex = 0;
this.TbUri.Text = "http://www.";
this.ButGet.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Right)));
this.ButGet.Location = new System.Drawing.Point(260, 10);
this.ButGet.Name = "ButGet";
this.ButGet.Size = new System.Drawing.Size(75, 23);
this.ButGet.TabIndex = 1;
this.ButGet.Text = "获取(&B)";
this.ButGet.UseVisualStyleBackColor = true;
this.ButGet.Click += new System.EventHandler(this.ButGet_Click);
this.TbResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) |
System.Windows.Forms.AnchorStyles.Right)));
this.TbResult.Location = new System.Drawing.Point(12, 143);
this.TbResult.MaxLength = 32767000;
this.TbResult.Multiline = true;
this.TbResult.Name = "TbResult";
this.TbResult.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.TbResult.Size = new System.Drawing.Size(323, 119);
this.TbResult.TabIndex = 2;
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 12);
this.label1.TabIndex = 3;
this.label1.Text = "Uri:";
this.RbAnonymous.AutoSize = true;
this.RbAnonymous.Checked = true;
this.RbAnonymous.Location = new System.Drawing.Point(6, 22);
this.RbAnonymous.Name = "RbAnonymous";
this.RbAnonymous.Size = new System.Drawing.Size(71, 16);
this.RbAnonymous.TabIndex = 4;
this.RbAnonymous.TabStop = true;
this.RbAnonymous.Text = "匿名方式";
this.RbAnonymous.UseVisualStyleBackColor = true;
this.RbWebService.AutoSize = true;
this.RbWebService.Location = new System.Drawing.Point(83, 22);
this.RbWebService.Name = "RbWebService";
this.RbWebService.Size = new System.Drawing.Size(113, 16);
this.RbWebService.TabIndex = 5;
this.RbWebService.Text = "Web Service方式";
this.RbWebService.UseVisualStyleBackColor = true;
this.GbGetMode.Controls.Add(this.RbGetData);
this.GbGetMode.Controls.Add(this.RbGetFile);
this.GbGetMode.Location = new System.Drawing.Point(14, 91);
this.GbGetMode.Name = "GbGetMode";
this.GbGetMode.Size = new System.Drawing.Size(321, 46);
this.GbGetMode.TabIndex = 6;
this.GbGetMode.TabStop = false;
this.GbGetMode.Text = "数据获取方式";
this.RbGetFile.AutoSize = true;
this.RbGetFile.Checked = true;
this.RbGetFile.Location = new System.Drawing.Point(6, 20);
this.RbGetFile.Name = "RbGetFile";
this.RbGetFile.Size = new System.Drawing.Size(71, 16);
this.RbGetFile.TabIndex = 5;
this.RbGetFile.Text = "获取文件";
this.RbGetFile.UseVisualStyleBackColor = true;
this.RbGetData.AutoSize = true;
this.RbGetData.Location = new System.Drawing.Point(83, 20);
this.RbGetData.Name = "RbGetData";
this.RbGetData.Size = new System.Drawing.Size(95, 16);
this.RbGetData.TabIndex = 6;
this.RbGetData.Text = "直接获取数据";
this.RbGetData.UseVisualStyleBackColor = true;
this.groupBox2.Controls.Add(this.RbAnonymous);
this.groupBox2.Controls.Add(this.RbWebService);
this.groupBox2.Location = new System.Drawing.Point(14, 39);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(321, 46);
this.groupBox2.TabIndex = 7;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "访问类型";
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(347, 274);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.GbGetMode);
this.Controls.Add(this.TbUri);
this.Controls.Add(this.label1);
this.Controls.Add(this.TbResult);
this.Controls.Add(this.ButGet);
this.Name = "FormMain";
this.ShowIcon = false;
this.Text = "Read File From Server by ";
this.GbGetMode.ResumeLayout(false);
this.GbGetMode.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.TextBox TbUri;
private System.Windows.Forms.Button ButGet;
private System.Windows.Forms.TextBox TbResult;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.RadioButton RbAnonymous;
private System.Windows.Forms.RadioButton RbWebService;
private System.Windows.Forms.GroupBox GbGetMode;
private System.Windows.Forms.RadioButton RbGetData;
private System.Windows.Forms.RadioButton RbGetFile;
private System.Windows.Forms.GroupBox groupBox2;
}
}

2012-11-19 16:06