日期:2013-05-08  浏览次数:20508 次

// 南京千里独行 2005-3-17

/// <summary>
/// 进度信息处理委托
/// </summary>
/// <param name="CompletedStep" type="int">已经完成的步骤数</param>
/// <param name="TotalStep" type="int">总的步骤数</param>
public delegate void ProgressHandler( int CompletedStep , int TotalStep );

/// <summary>
/// 通用函数集合
/// </summary>
public class YYFCommon
{
/// <summary>
/// 向指定URL使用POST方法发送数据的例程,本函数不进行错误处理
/// </summary>
/// <param name="strURL">URL字符串</param>
/// <param name="bytSend">要发送的二进制数据</param>
/// <param name="SendProgress">发送数据时的进度处理</param>
/// <param name="AcceptProgress">接受数据时的进度处理</param>
/// <returns>接受到的二进制数据</returns>
public static byte[] HttpPostData(
string strURL ,
byte[] bytSend ,
ProgressHandler SendProgress ,
ProgressHandler AcceptProgress )
{
// 发送数据
System.Net.HttpWebRequest myReq =(System.Net.HttpWebRequest) System.Net.WebRequest.Create( strURL );
myReq.Method = "POST" ;
System.IO.Stream myStream = myReq.GetRequestStream();
int iCount = 0 ;
if( SendProgress != null)
SendProgress( 0 , bytSend.Length );
while( iCount < bytSend.Length )
{
if( iCount + 1024 > bytSend.Length)
{
myStream.Write(bytSend, iCount , bytSend.Length - iCount );
iCount = bytSend.Length ;
}
else
{
myStream.Write(bytSend , iCount , 1024);
iCount += 1024;
}
if( SendProgress != null)
SendProgress( iCount , bytSend.Length );
}//while
if( SendProgress != null)
SendProgress( bytSend.Length , bytSend.Length );
myStream.Close();

// 接受数据
System.Net.HttpWebResponse myRes = null;
myRes = myReq.GetResponse() as System.Net.HttpWebResponse ;

myStream = myRes.GetResponseStream();
System.IO.MemoryStream myBuf = new System.IO.MemoryStream(1024);
byte[] bytBuf = new byte[1024];
int ContentLength = (int)myRes.ContentLength ;
int AcceptLength = 0 ;
if( AcceptProgress != null)
AcceptProgress(0 , ContentLength );
while(true)
{
int iLen = myStream.Read(bytBuf,0,1024);
if(iLen ==0)
break;
myBuf.Write(bytBuf,0,iLen);
AcceptLength += iLen ;
if( AcceptLength > ContentLength )
ContentLength = AcceptLength ;
if( AcceptProgress != null)
AcceptProgress( AcceptLength , ContentLength );
}//while
if( AcceptProgress != null)
AcceptProgress( AcceptLength , ContentLength );
myStream.Close();
myRes.Close();
myReq.Abort();
byte[] bytReturn = myBuf.ToArray();
myBuf.Close();
return bytReturn ;
}// public static byte[] HttpPostData()


/// <summary>
/// 根据保存在一个列表中的数据源参数修正字符串
/// </summary>
/// <param name="strText">供处理的原始字符串</param>
/// <param name="strHead">标记的头字符串</param>
/// <param name="strEnd">标记尾字符串</param>
/// <param name="myKeys">保存所有参数的列表</param>
/// <returns>处理后的字符串</returns>
public static string fixVariableString
( string strText,
string strHead,
string strEnd,
System.Collections.Hashtable myKeys )
{
// 若原始字符串无效或者没有任何可用的参数则退出函数
if( strText == null
|| strHead== null
|| strEnd == null
|| strHead.Length== 0
|| strEnd.Length== 0
|| strText.Length== 0
|| myKeys == null
|| myKeys.Count== 0 )
return strText ;

int index = strText.IndexOf( strHead );
// 若原始字符串没有变量标记则退出函数
if(index < 0 )
return strText ;

string strKey ;
int index2 ;
int LastIndex = 0 ;
System.Text.StringBuilder myStr = new System.Text.StringBuilder();
do
{
// 查找有 "[内容]" 样式的子字符串
// 若没有找到 "[&q