科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道应用软件调用有身份验证的文件传输Web Service

调用有身份验证的文件传输Web Service

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

先解决一下上篇文章提出的一个值得探讨的问题。上篇文章提到对纯二进制文件传输有些容量问题。

作者:曹勇刚 来源:yesky 2007年11月17日

关键字: 软件

  • 评论
  • 分享微博
  • 分享邮件
言归正传,我们来编写代码,首先,添加Web引用,确省的命名空间为localhost,当然,你可以改一下它,我们保留原样,你可以看到在类视图中可以localhost命名空间下多了两个类:Authentication和FileServer,如图3。当然,你看到我现在的FileServer有更多的功能,我们不贪多,就先注意一下GetFile相关的方法吧。


        图 3

  为了大家重现方便,把全部代码给出(鉴于篇幅太大,删去了些系统自动生成的注释),布局请注意InitializeComponent的代码,功能请注意button1_Click的内容。

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.IO;

namespace DownLoadFile

{

 ///

 /// Form1 is a Client For FileServer to Download File through HTTP

 ///


 public class Form1 : System.Windows.Forms.Form

 {

  private System.Windows.Forms.TextBox textBox2;//getfile input path

  private System.Windows.Forms.Panel panel3;

  private System.Windows.Forms.TextBox textBox3;//url of web service

  private System.Windows.Forms.Label label2;

  private System.Windows.Forms.Button button1;//getfile

  private System.Windows.Forms.Label label1;

  private System.Windows.Forms.TextBox textBox1;//username

  private System.Windows.Forms.Label label3;

  private System.Windows.Forms.TextBox textBox4;//password

  private System.Windows.Forms.SaveFileDialog saveFileDialog1;

  private System.ComponentModel.Container components = null;

  public Form1()

  {

   InitializeComponent();

  }

  protected override void Dispose( bool disposing )

  {

   if( disposing )

   {

    if (components != null)

    {

     components.Dispose();

    }

   }

   base.Dispose( disposing );

  }

  #region Windows Form Designer generated code

  private void InitializeComponent()

  {

   this.textBox2 = new System.Windows.Forms.TextBox();

   this.textBox3 = new System.Windows.Forms.TextBox();

   this.textBox1 = new System.Windows.Forms.TextBox();

   this.textBox4 = new System.Windows.Forms.TextBox();

   this.panel3 = new System.Windows.Forms.Panel();

   this.label2 = new System.Windows.Forms.Label();

   this.label1 = new System.Windows.Forms.Label();

   this.button1 = new System.Windows.Forms.Button();

   this.label3 = new System.Windows.Forms.Label();

   this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();

   this.panel3.SuspendLayout();

   this.SuspendLayout();

   this.textBox2.Location = new System.Drawing.Point(8, 32);

   this.textBox2.Name = "textBox2";

   this.textBox2.Size = new System.Drawing.Size(232, 21);

   this.textBox2.TabIndex = 1;

   this.textBox2.Text = "Please Input Path here";

   this.textBox3.Dock = System.Windows.Forms.DockStyle.Fill;

   this.textBox3.Location = new System.Drawing.Point(56, 0);

   this.textBox3.Name = "textBox3";

   this.textBox3.Size = new System.Drawing.Size(328, 21);

   this.textBox3.TabIndex = 2;

   this.textBox3.Text = "http://localhost/sei/test/useresdata/FileServer.asmx";

   this.textBox1.Location = new System.Drawing.Point(80, 64);

   this.textBox1.Name = "textBox1";

   this.textBox1.Size = new System.Drawing.Size(256, 21);

   this.textBox1.TabIndex = 5;

   this.textBox1.Text = "caomo";

   this.textBox4.Location = new System.Drawing.Point(80, 96);

   this.textBox4.Name = "textBox4";

   this.textBox4.PasswordChar = '*';

   this.textBox4.Size = new System.Drawing.Size(256, 21);

   this.textBox4.TabIndex = 7;

   this.textBox4.Text = "password";

   this.panel3.Controls.AddRange(new System.Windows.Forms.Control[] {

   this.textBox3,

   this.label2});

   this.panel3.Dock = System.Windows.Forms.DockStyle.Top;

   this.panel3.Name = "panel3";

   this.panel3.Size = new System.Drawing.Size(384, 24);

   this.panel3.TabIndex = 2;

   this.label2.Dock = System.Windows.Forms.DockStyle.Left;

   this.label2.Name = "label2";

   this.label2.Size = new System.Drawing.Size(56, 24);

   this.label2.TabIndex = 1;

   this.label2.Text = "Web服务:";

   this.label1.Location = new System.Drawing.Point(8, 72);

   this.label1.Name = "label1";

   this.label1.Size = new System.Drawing.Size(72, 16);

   this.label1.TabIndex = 4;

   this.label1.Text = "UserName:";

   this.button1.Location = new System.Drawing.Point(256, 32);

   this.button1.Name = "button1";

   this.button1.Size = new System.Drawing.Size(72, 24);

   this.button1.TabIndex = 3;
        
   this.button1.Text = "GetFile";

   this.button1.Click += new System.EventHandler(this.button1_Click);

   this.label3.Location = new System.Drawing.Point(8, 96);

   this.label3.Name = "label3";

   this.label3.Size = new System.Drawing.Size(64, 16);

   this.label3.TabIndex = 6;

   this.label3.Text = "Password";

   this.saveFileDialog1.FileName = "doc1";

   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

   this.ClientSize = new System.Drawing.Size(384, 125);

   this.Controls.AddRange(new System.Windows.Forms.Control[] {

   this.textBox4,

   this.label3,

   this.textBox1,

   this.label1,

   this.button1,

   this.panel3,

   this.textBox2});

   this.Name = "Form1";

   this.Text = "DownLoad File From Web Service";

   this.panel3.ResumeLayout(false);

   this.ResumeLayout(false);

  }

  #endregion

  ///

  /// The main entry point for the application.

  ///


  [STAThread]

  static void Main()

  {

   Application.Run(new Form1());

  }

  private void button1_Click(object sender, System.EventArgs e)

  {

   localhost.Authentication auth=new localhost.Authentication();

   auth.Username =textBox1.Text;

   auth.Password=textBox4.Text;

   localhost.FileServer fs=new localhost.FileServer();

   fs.Url=textBox3.Text;

   fs.AuthenticationValue=auth;

   string s=null;

   try

   {

    s=fs.GetFile(textBox2.Text);

    if (s==null) MessageBox.Show("错误:请检查用户名和密码是否正确!","错误:");

    else

    {

     byte[] btBuf = Convert.FromBase64String(s);

     int i=textBox2.Text.LastIndexOf("\\");

     saveFileDialog1.FileName =textBox2.Text.Substring (i+1,textBox2.Text.Length-i-1);

     DialogResult drs=saveFileDialog1.ShowDialog();

     if (drs==DialogResult.OK)

     {

     FileStream fstream = new FileStream(saveFileDialog1.FileName , FileMode.OpenOrCreate, FileAccess.Write);

     BinaryWriter w = new BinaryWriter(fstream);

     w.Write (btBuf);

     w.Flush();

     fstream.Close();

   }

  }

 }

 catch

 {

  MessageBox.Show("错误:请检查网络是否连通或网络地址是否正确。","错误:");

 }

 }

 }

 }

  编程、调试加书写,饿死了。为了大家理解,再多说几句吧。在button1_Click中,灰底的部分是主要的生成调用用户验证web service的对象的代码。后面有许多异常处理是很有必要的。最后,应该教会了你如何利用存储对话框和文件系统存储文件。

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章