WebService中传2进制文件(ASP.NET C#实战例子)

本文主要展示了使用WebService获取宾馆酒店经理资格证书信息的关键代码。定义了相关数据结构,包含资格证书和二进制图片信息。通过WebMethod获取数据,对Soap Header进行加密/解密,还给出了获取二进制文件的方法,最后提供非Dotnet程序调用的参考信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function StorePage() { d=document; t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():''); void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes')); keyit.focus(); }

实战的代码太多了,我只帖关键部分,并且,注释一下,主要是为了自己以后备忘,注意红字部分!实战应用需要对Soap Header进行加密/解密,

加密/解密文章参考:http://blog.youkuaiyun.com/hnwanghb/archive/2005/03/19/323869.aspx

首先定义一个数据结构:

里面还嵌套着一个struRemoteFile 结构!

/// <summary>
/// 宾馆酒店经理资格证书的数据结构
/// 包含2进制图片信息
/// </summary>

public struct structHotelCertified
{
public string ID; //记录序列号/
public string Certified_ID; //资格证书号码/
public string Name; //姓名/
public string Sex; //性别/
public string Education_ID; //学历/
public string Identity_Card; //身份证/
public string Unit_Name; //单位名称/
public string Obtain_Year; //获证年度/
public string Establish_Time; //最后修改时间/

public struRemoteFile rmtPicFile; //照片 (注意:2进制文件的结构)
}

/// <summary>
/// 传送2进制文件的数据结构
/// string FileName; //文件名
/// byte[] Contents; //文件内容
/// DateTime CreationTime; //文件创建时间
/// DateTime LastAccessTime; //文件最后访问时间
/// DateTime LastWriteTime; //文件最后修改时间
/// </summary>

public struct struRemoteFile
{
public string FileName; //文件名
public byte[] Contents; //文件内容
public DateTime CreationTime; //文件创建时间
public DateTime LastAccessTime; //文件最后访问时间
public DateTime LastWriteTime; //文件最后修改时间
}

/// <summary>
/// 功能描述:获取旅游从业人员的资格证书信息[宾馆酒店经理]
/// 参数说明:lastID—客户端已经更新的最新ID号,当前操作需要获取ID号大于lastID的记录
/// 参数说明:number—获取数据的记录数量[Web传输中数据过大会产生异常,因此需要限制一次更新的数据数量]
/// </summary>

[WebMethod(Description="获取宾馆酒店经理资格证书的信息[包括图片文件]")]
[SoapHeader("AuthenHeader",Direction=SoapHeaderDirection.In,Required=true)] //身份验证代码
public structHotelCertified[] WebGetHotelCertified(int lastID,int number)
{
try
{
//数据输出对象
THRDataExport objDataExport=new THRDataExport(objHRLogin.NewConnection());

structHotelCertified[] pHotelCertified;

//如果验证通过,获取宾馆酒店经理资料
if(!bAuthenUser)
{
if(!AuthenHeader.ValidUser(AuthenHeader.Username,AuthenHeader.Password,objHRLogin.DBConnection))
{
return null;
}
}

pHotelCertified=objDataExport.GetHotelCertifiedInfo(lastID,strHotelPicPath,number) ; //把包含2 进制的结构对象返回的调用WebService的程序

//代码注释:objDataExport是一类THRDataExport的一个实例。

return pHotelCertified;

}
catch(Exception e)
{
ErrorMessage=e.Message;
return null;
}
}

类THRDataExport中的相关代码:

/// <summary>
/// 功能描述:获取最新的宾馆酒店经理资格证书信息
/// <param name="lastID">lastID—客户端已经更新的最新ID号,当前操作需要获取ID号大于lastID的记录</param>
/// <param name="FilePath">图片存放路径</param>
/// <param name="GetNum">获取记录数量</param>
/// <returns>structHotelCertified结构数组</returns>
public structHotelCertified[] GetHotelCertifiedInfo(int lastID,string FilePath,int GetNum)
{
try
{
string strSQL="";
int iCount=0;

DataSet ds=new DataSet();

//=========================== 获取宾馆酒店经理资格证书的数据 Start ================================

//将所有的服务器端的数据取出,导入dds,用dds Tables[0]中的差异记录对服务器端的数据全集进行过滤

strSQL="Select * From T_HR_Hotel_Certified Where ID>"+lastID.ToString()+" And rownum<="+GetNum.ToString()+" Order By ID Asc";

OleDbDataAdapter dtHotel=new OleDbDataAdapter(strSQL,Cn);

//填充记录集
dtHotel.Fill(ds,"T_HR_Hotel_Certified");

iCount=ds.Tables["T_HR_Hotel_Certified"].Rows.Count; //获取满足条件的宾馆酒店经理资格证书记录数量

if(iCount>0)
{
structHotelCertified[] pHotelCert=new structHotelCertified[iCount];

for(int m=0;m<ds.Tables["T_HR_Hotel_Certified"].Rows.Count;m++)
{
string strFileName;
strFileName=FilePath+ds.Tables["T_HR_Hotel_Certified"].Rows[m]["Certified_ID"].ToString()+".jpg";

pHotelCert[m].ID= ds.Tables["T_HR_Hotel_Certified"].Rows[m]["ID"].ToString();
pHotelCert[m].Certified_ID= ds.Tables["T_HR_Hotel_Certified"].Rows[m]["Certified_ID"].ToString();
pHotelCert[m].Education_ID= ds.Tables["T_HR_Hotel_Certified"].Rows[m]["Education_ID"].ToString();
pHotelCert[m].Establish_Time= ds.Tables["T_HR_Hotel_Certified"].Rows[m]["Establish_Time"].ToString();
pHotelCert[m].Identity_Card= ds.Tables["T_HR_Hotel_Certified"].Rows[m]["Identity_Card"].ToString();
pHotelCert[m].Name=ds.Tables["T_HR_Hotel_Certified"].Rows[m]["Name"].ToString();
pHotelCert[m].Obtain_Year=ds.Tables["T_HR_Hotel_Certified"].Rows[m]["Obtain_Year"].ToString();
pHotelCert[m].Sex=ds.Tables["T_HR_Hotel_Certified"].Rows[m]["Sex"].ToString();
pHotelCert[m].Unit_Name=ds.Tables["T_HR_Hotel_Certified"].Rows[m]["Unit_Name"].ToString();

//--------------------------- 获取宾馆酒店经理照片信息 Start----------------------------
pHotelCert[m].rmtPicFile=GetBinaryFile(@strFileName); //读取2进制文件
//--------------------------- 获取宾馆酒店经理照片信息 End----------------------------

}

return pHotelCert;
}
else
{
return null;
}
//=========================== 获取宾馆酒店经理资格证书的数据 End ================================

}
catch(OleDbException oe)
{
ErrorMessage=oe.Message;
return null;
}
finally
{

}
}

/// <summary>
/// 获取指定文件名称[全路径名称如: c:\temp\data\12367.jpg]的2进制文件
/// </summary>
/// <returns>2进制数组</returns>
private struRemoteFile GetBinaryFile(string FileName)
{
struRemoteFile pRmtFile=new struRemoteFile();

if(File.Exists(@FileName))
{
//读取文件的内容
Stream fs=File.Open(FileName,FileMode.Open);

pRmtFile.Contents=new byte[fs.Length];

fs.Read(pRmtFile.Contents,0,(int)fs.Length);

fs.Close();

//获取文件的时间参数

pRmtFile.CreationTime=File.GetCreationTime(FileName);
pRmtFile.LastAccessTime=File.GetLastAccessTime(FileName);
pRmtFile.LastWriteTime=File.GetLastWriteTime(FileName);

}

return pRmtFile;

}

最后注意:如果是非Dotnet程序编码调用,参考以下的WebService的结构信息。参考URL:http://www.tttsss.com/webservice/THRDataService.asmx?op=WebGetHotelCertified

DataExchange


Click here for a complete list of operations.

WebGetHotelCertified

获取宾馆酒店经理资格证书的信息[包括图片文件]

Test

The test form is only available for requests from the local machine.

SOAP

The following is a sample SOAP request and response. The placeholders shown need to be replaced with actual values.

POST /webservice/THRDataService.asmx HTTP/1.1
Host: www.tttsss.com
Content-Type: text/xml; charset=utf-8
Content-Length: 
SOAPAction: "http://www.tttsss.com/webservice/WebGetHotelCertified"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <Authentication xmlns="http://www.tttsss.com/webservice/">
      <Username></Username>
      <Password></Password>
      <Key></Key>
      <IV></IV>
      <curAlgorithm> or  or  or </curAlgorithm>
    </Authentication>
  </soap:Header>
  <soap:Body>
    <WebGetHotelCertified xmlns="http://www.tttsss.com/webservice/">
      <lastID></lastID>
      <number></number>
    </WebGetHotelCertified>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <WebGetHotelCertifiedResponse xmlns="http://www.tttsss.com/webservice/">
      <WebGetHotelCertifiedResult>
        <structHotelCertified>
          <ID></ID>
          <Certified_ID></Certified_ID>
          <Name></Name>
          <Sex></Sex>
          <Education_ID></Education_ID>
          <Identity_Card></Identity_Card>
          <Unit_Name></Unit_Name>
          <Obtain_Year></Obtain_Year>
          <Establish_Time></Establish_Time>
          <rmtPicFile>
            <FileName></FileName>
            <Contents></Contents>
            <CreationTime></CreationTime>
            <LastAccessTime></LastAccessTime>
            <LastWriteTime></LastWriteTime>
          </rmtPicFile>
        </structHotelCertified>
        <structHotelCertified>
          <ID></ID>
          <Certified_ID></Certified_ID>
          <Name></Name>
          <Sex></Sex>
          <Education_ID></Education_ID>
          <Identity_Card></Identity_Card>
          <Unit_Name></Unit_Name>
          <Obtain_Year></Obtain_Year>
          <Establish_Time></Establish_Time>
          <rmtPicFile>
            <FileName></FileName>
            <Contents></Contents>
            <CreationTime></CreationTime>
            <LastAccessTime></LastAccessTime>
            <LastWriteTime></LastWriteTime>
          </rmtPicFile>
        </structHotelCertified>
      </WebGetHotelCertifiedResult>
    </WebGetHotelCertifiedResponse>
  </soap:Body>
</soap:Envelope>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值