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

来源:互联网 发布:php整数转换成小数 编辑:程序博客网 时间:2024/06/06 13:09
 

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

加密/解密文章参考:http://blog.csdn.net/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.1Host: www.tttsss.comContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "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>base64Binary</Username>      <Password>base64Binary</Password>      <Key>base64Binary</Key>      <IV>base64Binary</IV>      <curAlgorithm>Des or Rc2 or Rijndael or TripleDes</curAlgorithm>    </Authentication>  </soap:Header>  <soap:Body>    <WebGetHotelCertified xmlns="http://www.tttsss.com/webservice/">      <lastID>int</lastID>      <number>int</number>    </WebGetHotelCertified>  </soap:Body></soap:Envelope>
HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: 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>string</ID>          <Certified_ID>string</Certified_ID>          <Name>string</Name>          <Sex>string</Sex>          <Education_ID>string</Education_ID>          <Identity_Card>string</Identity_Card>          <Unit_Name>string</Unit_Name>          <Obtain_Year>string</Obtain_Year>          <Establish_Time>string</Establish_Time>          <rmtPicFile>            <FileName>string</FileName>            <Contents>base64Binary</Contents>            <CreationTime>dateTime</CreationTime>            <LastAccessTime>dateTime</LastAccessTime>            <LastWriteTime>dateTime</LastWriteTime>          </rmtPicFile>        </structHotelCertified>        <structHotelCertified>          <ID>string</ID>          <Certified_ID>string</Certified_ID>          <Name>string</Name>          <Sex>string</Sex>          <Education_ID>string</Education_ID>          <Identity_Card>string</Identity_Card>          <Unit_Name>string</Unit_Name>          <Obtain_Year>string</Obtain_Year>          <Establish_Time>string</Establish_Time>          <rmtPicFile>            <FileName>string</FileName>            <Contents>base64Binary</Contents>            <CreationTime>dateTime</CreationTime>            <LastAccessTime>dateTime</LastAccessTime>            <LastWriteTime>dateTime</LastWriteTime>          </rmtPicFile>        </structHotelCertified>      </WebGetHotelCertifiedResult>    </WebGetHotelCertifiedResponse>  </soap:Body></soap:Envelope>