前几天一直在调一段代码,它是抛出不支持application/dime的请求类型,虽然网上有这方面的问题,但是说的不是很清楚,最后我发现WSE2.0只能在.net2003上使用,于是环境是配好了
下面是我下的代码两个函数,一个是用于处理上传文件,一个是下载文件。
///这是我的项目里的几段代码
//这个函数的主要作用是将东西备份到自己的电脑上
private void BackupToYourComputer()
{
//WebBackReReference是我添加的一个Webservice引用
WSClientWSE.WebBackReReference.AcsBackuprestoreServiceWse service = new WSClientWSE.WebBackReReference.AcsBackuprestoreServiceWse();
service.Url = WSClientWSE.WebServiceCommon.WebServiceSetting.Location;
WSClientWSE.WebBackReReference.FileInfo3 fileinfo3 = new SClientWSE.WebBackReReference.FileInfo3();
if(!this.textBoxBackTComFileName.Text.Equals(string.Empty))
{
fileinfo3.FileName = this.textBoxBackTComFileName.Text;
}
else
{
MessageBox.Show("The Filename is null.");
return;
}
if(!this.textBoxBackTComFileOwner.Text.Equals(string.Empty))
{
fileinfo3.FileOwner = this.textBoxBackTComFileOwner.Text;
}
else
{
MessageBox.Show("The FileOwner is null.");
return;
}
if(!this.textBoxBackTComDes.Text.Equals(string.Empty))
{
fileinfo3.FileDescription = this.textBoxBackTComDes.Text;
}
else
{
MessageBox.Show("The Description is null.");
return;
}
WSClientWSE.WebBackReReference.DataFileStruct date = new WSClientWSE.WebBackReReference.DataFileStruct();
try
{
date = service.BackupToYourComputer(fileinfo3);
}
catch(SoapException ex)
{
this.listBoxLog.Items.Add(WSClientWSE.WebServiceCommon.WebServiceSetting.getTimeTag() + ex.Message);
if(ex.Detail != null)
{
this.listBoxLog.Items.Add(ex.Detail.OuterXml);
}
}
//设置saveFileDialog
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.Title = "BackupToYourComputer";
if(!date.FileName.Equals(null))
{
saveFileDialog1.FileName = date.FileName + ".zip";
}
else
{
MessageBox.Show("The filename is null.");
return;
}
//下面是将附件存在本机上
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Stream stream = service.ResponseSoapContext.Attachments[0].Stream;
int length = (int)stream.Length;
byte[] attdate = new byte[length];
stream.Read(attdate,0,length);
string filepath = saveFileDialog1.FileName;
FileStream fs = System.IO.File.Create(filepath);
fs.Write(attdate,0,length);
fs.Flush();
fs.Close();
}
}
//这个函数的主要作用是纪录打开文件的绝对路径,这在下面WSE添加附件是使用
private void buttonReFComAddFile_Click(object sender, System.EventArgs e)
{
G_FilePath = null;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
if(!openFileDialog1.FileName.Equals(string.Empty))
{
G_FilePath = this.openFileDialog1.FileName;
this.textBoxReFComFileName.Enabled = true;
}
else
{
MessageBox.Show("The File is null.");
return;
}
}
}
private void RestoreFromYourComputer()
{
WSClientWSE.WebBackReReference.AcsBackuprestoreServiceWse service = new WSClientWSE.WebBackReReference.AcsBackuprestoreServiceWse();
service.Url = WSClientWSE.WebServiceCommon.WebServiceSetting.Location;
string filename,user;
if(!this.textBoxReFComFileName.Text.Equals(string.Empty))
{
filename = this.textBoxReFComFileName.Text;
this.textBoxReFComFileName.Enabled = false;
}
else
{
MessageBox.Show("The Filename is null.");
return;
}
if(!this.textBoxReFComFileOwner.Text.Equals(string.Empty))
{
user = this.textBoxReFComFileOwner.Text;
}
else
{
MessageBox.Show("The Owner is null.");
return;
}
if(!G_FilePath.Equals(string.Empty))
{
//添加附件
DimeAttachment att = new DimeAttachment("text/xml",Microsoft.Web.Services2.Dime.TypeFormat.None,G_FilePath);
service.RequestSoapContext.Attachments.Add(att);
string result;
try
{
result = service.RestoreFromYourComputer(filename,user);
if(!result.Equals(string.Empty))
{
this.listBoxLog.Items.Add(WSClientWSE.WebServiceCommon.WebServiceSetting.getTimeTag() + result);
}
else
{
this.listBoxLog.Items.Add(WSClientWSE.WebServiceCommon.WebServiceSetting.getTimeTag() + "The result is null.");
}
}
catch(SoapException ex)
{
this.listBoxLog.Items.Add(WSClientWSE.WebServiceCommon.WebServiceSetting.getTimeTag() +ex.Message);
System.Xml.XmlNode xmlnode = ex.Detail;
if(xmlnode != null)
{
this.listBoxLog.Items.Add(xmlnode.OuterXml);
}
}
catch(Exception ex)
{
this.listBoxLog.Items.Add(WSClientWSE.WebServiceCommon.WebServiceSetting.getTimeTag() + ex.Message);
}
}
else
{
MessageBox.Show("Please select a file first.");
}
}
虽然这是很简单的,但是我还是花费了好长时间才搞懂的,本人是个菜鸟级别的人物,^_^。