分享
 
 
 

2004年最后一天的原创:C#通过HTTP操作数据的模块

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

// 本程序提供了一个通过HTTP来操作数据库的类,这样少了本地安装乱七八糟的数据库驱动程序及复杂的设置,

// Web 服务器可以是任意的类型的,可以为 ASP , ASP.NET JSP 或其他,本模块提供了服务器的ASP.NET 的

// 例子

// 南京千里独行原创 请引用时勿删除本行 2004-12-31

using System;

using System.Net ;

namespace myConnection

{

///

/// XMLHttp数据库连接操作事件处理委托

///

/// 数据库连接对象

/// 数据库命令对象

/// 总的数据字节数

/// 已完成的数据字节数

public delegate void XMLHttpDBExecutingHandler( XMLHttpConnection conn , XMLHttpCommand cmd , long ContentLength , long ContentCompleted );

///

/// XMLHttp类型的数据库数据读取对象

///

public class XMLHttpReader : System.Data.IDataReader

{

private string[] strHead = null;

private System.Collections.ArrayList myRows = new System.Collections.ArrayList();

private bool bolClosed = false;

private int intRowIndex = 0 ;

private const string c_NullFlag = "[NULL]" ;

///

/// 根据字段名称获得字段序号,比较不区分大小写

///

/// 字段名称

/// 字段的从0开始的序号,若没找到则返回-1

public int GetIndexByName( string strName )

{

if( strName == null ) return -1 ;

strName = strName.ToUpper().Trim();

for( int iCount = 0 ; iCount

/// 从一个数据库读取对象加载对象数据

///

/// 数据库数据读取对象

/// 加载的记录的行数

public int FromReader(System.Data.IDataReader myReader)

{

if( myReader != null)

{

// 加载列头信息

strHead = new string[ myReader.FieldCount ];

for( int iCount = 0 ; iCount

/// 保存对象数据到XML节点

///

/// 根XML节点

/// 保存的记录个数

public int ToXML( System.Xml.XmlElement RootElement )

{

System.Xml.XmlElement RowElement = null;

System.Xml.XmlElement FieldElement = null;

while( RootElement.FirstChild != null)

RootElement.RemoveChild( RootElement.FirstChild);

// 保存列数据

if( strHead != null && strHead.Length 0 )

{

for( int iCount = 0 ; iCount 0 )

{

System.Xml.XmlDocument myDoc = RootElement.OwnerDocument ;

for( int RowCount = 0 ; RowCount

/// 从XML元素加载对象数据

///

/// XML节点

/// 加载的数据的行数

public int FromXML( System.Xml.XmlElement RootElement )

{

System.Xml.XmlElement RowElement = null;

strHead = null;

myRows.Clear();

intRowIndex = -1 ;

if( RootElement == null ) return -1 ;

// 获得列信息

System.Collections.ArrayList myHeads = new System.Collections.ArrayList();

for( int iCount = 0 ; iCount

/// 未实现

///

public int RecordsAffected

{

get {return 0; }

}

///

/// 已重载:关闭对象

///

public bool IsClosed

{

get{ return bolClosed ; }

}

///

/// 未实现

///

///

public bool NextResult()

{

if( myRows == null || (intRowIndex+1) = myRows.Count )

return false;

else

return true;

}

///

/// 已重载:关闭对象

///

public void Close()

{

bolClosed = true;

strHead = null;

myRows = null;

}

///

/// 移动当前记录到下一行

///

///

public bool Read()

{

intRowIndex ++ ;

if( intRowIndex = myRows.Count )

return false;

else

return true;

}

///

/// 已重载:不支持

///

public int Depth

{

get{ return 0; }

}

///

/// 已重载:不支持

///

///

public System.Data.DataTable GetSchemaTable()

{

// TODO: 添加 XMLHttpReader.GetSchemaTable 实现

return null;

}

#endregion

#region IDisposable 成员

public void Dispose()

{

// TODO: 添加 XMLHttpReader.Dispose 实现

}

#endregion

#region IDataRecord 成员

///

/// 已重载:获得指定列号的整数型数据

///

/// 从0开始的列号

/// 转换成功的整形数据

public int GetInt32(int i)

{

return Convert.ToInt32( this.GetString(i));

}

///

/// 获得指定列名的数据

///

public object this[string name]

{

get

{

return this.GetString( this.GetIndexByName( name));

}

}

///

/// 获得指定列号的数据

///

object System.Data.IDataRecord.this[int i]

{

get

{

return this.GetString(i);

}

}

///

/// 获得指定列号的数据

///

/// 从0开始的列号

/// 获得的数据

public object GetValue(int i)

{

return GetString(i);

}

///

/// 判断指定列号的数据是否为空

///

/// 从0开始的列号

/// 是否为空

public bool IsDBNull(int i)

{

return GetString(i) == null;

}

public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)

{

byte[] bytData = Convert.FromBase64String( GetString(i));

int iCount = 0 ;

for( ; iCount

/// 获得指定列的字节数据

///

///

///

public byte GetByte(int i)

{

return Convert.ToByte( GetString(i));

}

///

/// 未实现

///

///

///

public Type GetFieldType(int i)

{

return null;

}

///

/// 获得数值数据

///

///

///

public decimal GetDecimal(int i)

{

return Convert.ToDecimal( this.GetString( i));

}

///

/// 未实现

///

///

///

public int GetValues(object[] values)

{

// TODO: 添加 XMLHttpReader.GetValues 实现

return 0;

}

///

/// 已重载:返回指定列的名称

///

///

///

public string GetName(int i)

{

if( i = 0 && i

/// 已重载:返回列数

///

public int FieldCount

{

get

{

return strHead.Length ;

}

}

///

/// 获得长整形数据

///

///

///

public long GetInt64(int i)

{

return Convert.ToInt64( GetString(i));

}

///

/// 获得双精度浮点数

///

///

///

public double GetDouble(int i)

{

return Convert.ToDouble( GetString(i));

}

///

/// 获得布尔类型数据

///

///

///

public bool GetBoolean(int i)

{

return Convert.ToBoolean( GetString(i));

}

///

/// 未实现

///

///

///

public Guid GetGuid(int i)

{

// TODO: 添加 XMLHttpReader.GetGuid 实现

return new Guid ();

}

///

/// 获得时间类型数据

///

///

///

public DateTime GetDateTime(int i)

{

return Convert.ToDateTime( GetString(i));

}

///

/// 未实现

///

///

///

public int GetOrdinal(string name)

{

return 0;

}

///

/// 未实现

///

///

///

public string GetDataTypeName(int i)

{

return null;

}

///

/// 获得单精度浮点数

///

///

///

public float GetFloat(int i)

{

return Convert.ToSingle( GetString(i));

}

///

/// 未实现

///

///

///

public System.Data.IDataReader GetData(int i)

{

return null;

}

///

/// 未实现

///

///

///

///

///

///

///

public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)

{

return 0;

}

///

/// 获得字符串类型数据

///

///

///

public string GetString(int i)

{

if( intRowIndex = 0 && intRowIndex = 0 && i

/// 获得字符类型数据

///

///

///

public char GetChar(int i)

{

return Convert.ToChar( GetString(i));

}

///

/// 获得短整型数据

///

///

///

public short GetInt16(int i)

{

return Convert.ToInt16( GetString(i));

}

#endregion

}// class XMLHttpReader

//*******************************************************************************************

//*******************************************************************************************

///

/// XMLHttp数据库处理对象使用的参数集合

///

public class XMLHttpParameterCollection : System.Data.IDataParameterCollection

{

private System.Collections.ArrayList myParams = new System.Collections.ArrayList();

#region IDataParameterCollection 成员

///

/// 返回指定名称的参数对象

///

public object this[string parameterName]

{

get

{

foreach( XMLHttpParameter myParam in myParams )

if( myParam.ParameterName == parameterName )

return myParam ;

return null;

}

set

{

// TODO: 添加 XMLHttpParameterCollection.this setter 实现

}

}

public void RemoveAt(string parameterName)

{

object obj = this[parameterName];

if( obj != null)

myParams.Remove( obj );

}

public bool Contains(string parameterName)

{

return ( this[parameterName] != null);

}

public int IndexOf(string parameterName)

{

foreach( XMLHttpParameter myParam in myParams )

if( myParam.ParameterName == parameterName )

return myParams.IndexOf( myParam ) ;

return -1 ;

}

#endregion

#region IList 成员

public bool IsReadOnly

{

get

{

// TODO: 添加 XMLHttpParameterCollection.IsReadOnly getter 实现

return false;

}

}

object System.Collections.IList.this[int index]

{

get

{

return myParams[index];

}

set

{

myParams[index] = value;

}

}

void System.Collections.IList.RemoveAt(int index)

{

myParams.RemoveAt( index );

}

public void Insert(int index, object obj)

{

myParams.Insert( index , obj );

// TODO: 添加 XMLHttpParameterCollection.Insert 实现

}

int System.Collections.IList.IndexOf( object obj)

{

return myParams.IndexOf( obj );

}

public void Remove(object obj)

{

myParams.Remove( obj );

}

bool System.Collections.IList.Contains(object obj)

{

return myParams.Contains( obj );

}

public void Clear()

{

myParams.Clear();

}

public int Add(object obj)

{

myParams.Add( obj );

return 0 ;

}

public bool IsFixedSize

{

get

{

// TODO: 添加 XMLHttpParameterCollection.IsFixedSize getter 实现

return false;

}

}

#endregion

#region ICollection 成员

public bool IsSynchronized

{

get

{

// TODO: 添加 XMLHttpParameterCollection.IsSynchronized getter 实现

return false;

}

}

public int Count

{

get

{

return myParams.Count ;

}

}

public void CopyTo(Array array, int index)

{

// TODO: 添加 XMLHttpParameterCollection.CopyTo 实现

}

public object SyncRoot

{

get

{

// TODO: 添加 XMLHttpParameterCollection.SyncRoot getter 实现

return null;

}

}

#endregion

#region IEnumerable 成员

public System.Collections.IEnumerator GetEnumerator()

{

return myParams.GetEnumerator();

}

#endregion

}

//********************************************************************************

//********************************************************************************

///

/// XMLHttp数据库连接对象使用的参数类型

///

public class XMLHttpParameter : System.Data.IDataParameter

{

private System.Data.ParameterDirection intDirection = System.Data.ParameterDirection.Input ;

private System.Data.DataRowVersion intSourceVersion = System.Data.DataRowVersion.Default ;

private System.Data.DbType intDbType = System.Data.DbType.String ;

private object objValue = null;

private string strParameterName = null;

private string strSourceColumn = null;

///

/// 用指定字符串数据初始化对象

///

///

public XMLHttpParameter( string strValue)

{

objValue = strValue ;

}

///

/// 无参数的初始化对象

///

public XMLHttpParameter()

{}

#region IDataParameter 成员

///

/// 获取或设置一个值,该值指示参数是只可输入、只可输出、双向还是存储过程返回值参数。

///

public System.Data.ParameterDirection Direction

{

get{ return intDirection ;}

set{ intDirection = value;}

}

///

/// 获取或设置参数的 System.Data.DbType

///

public System.Data.DbType DbType

{

get{ return intDbType ;}

set{ intDbType = value ;}

}

///

/// 获取或设置该参数的值

///

public object Value

{

get{ return objValue ;}

set{ objValue = value;}

}

public bool IsNullable

{

get

{

return false;

}

}

public System.Data.DataRowVersion SourceVersion

{

get{ return intSourceVersion ;}

set{ intSourceVersion = value;}

}

///

/// 参数名称

///

public string ParameterName

{

get{ return strParameterName ;}

set{ strParameterName = value;}

}

///

/// 参数栏目名称

///

public string SourceColumn

{

get{ return strSourceColumn ;}

set{ strSourceColumn = value;}

}

#endregion

}

//*************************************************************************************

//*************************************************************************************

///

/// 使用XMLHttp进行数据库访问的命令对象

///

public class XMLHttpCommand : System.Data.IDbCommand

{

private System.Data.CommandType intCommandType = System.Data.CommandType.Text ;

private System.Data.CommandBehavior intCmdBehavior = System.Data.CommandBehavior.Default ;

private System.Data.UpdateRowSource intUpdatedRowSource = System.Data.UpdateRowSource.Both ;

private int intCommandTimeout = 0 ;

private bool bolCancel = false;

private string strCommandText = null;

///

/// 对象执行类型 0:查询数据库 1:更新数据库

///

private int intExecuteType = 0 ;

private XMLHttpConnection myConnection = null;

private XMLHttpParameterCollection myParameters = new XMLHttpParameterCollection();

private int intExecuteState = 0 ;

private System.Xml.XmlDocument myXMLDoc = new System.Xml.XmlDocument();

private const string c_NullFlag = "[NULL]" ;

///

/// 正在执行命令事件处理

///

public XMLHttpDBExecutingHandler ExecuteEvent = null;

private string strHttpMethod = "POST";

///

/// 正在执行的状态 0:没有执行 1:正在发送数据 2:正在接受数据 3:正在解析数据

///

public int ExecuteState

{

get{ return intExecuteState ;}

}

///

/// 向HTTP服务器提交数据的方法

///

public string HttpMethod

{

get{ return strHttpMethod ;}

set{ strHttpMethod = value;}

}

///

/// 保存对象数据到一个XML节点中

///

///

///

public bool FromXML(System.Xml.XmlElement myElement )

{

if( myElement != null)

{

intExecuteType = StringCommon.ToInt32Value( myElement.GetAttribute("type"),0);

strCommandText = myElement.GetAttribute("text");

myParameters.Clear();

foreach( System.Xml.XmlNode myNode in myElement.ChildNodes)

{

if( myNode.Name == "param" )

{

System.Xml.XmlElement myPElement = myNode as System.Xml.XmlElement ;

XMLHttpParameter myParam = new XMLHttpParameter();

myParam.ParameterName = myPElement.GetAttribute("name");

myParam.Value = myPElement.InnerText ;

myParameters.Add( myParam );

}

}

}

return true;

}

///

/// 从一个XML节点加载对象数据

///

///

///

public bool ToXML( System.Xml.XmlElement myElement )

{

if( myElement != null)

{

myElement.SetAttribute("text", strCommandText);

myElement.SetAttribute("type", intExecuteType.ToString());

foreach( XMLHttpParameter myParam in myParameters )

{

System.Xml.XmlElement myPElement = myElement.OwnerDocument.CreateElement("param");

myElement.AppendChild( myPElement );

myPElement.SetAttribute("name" , myParam.ParameterName );

myPElement.InnerText =( myParam.Value == null ? c_NullFlag : myParam.Value.ToString());

}

}

return true;

}

///

/// 发送并接受二进制数据

///

///

///

internal byte[] SendByteData( byte[] bytSend)

{

// 发送数据

System.Net.HttpWebRequest myReq = myConnection.CreateHttpRequest();

System.IO.Stream myStream = myReq.GetRequestStream();

int iCount = 0 ;

bolCancel = false;

intExecuteState = 1 ;

while( iCount bytSend.Length)

{

myStream.Write(bytSend, iCount , bytSend.Length - iCount );

iCount = bytSend.Length ;

}

else

{

myStream.Write(bytSend , iCount , 1024);

iCount += 1024;

}

if( ExecuteEvent != null)

{

ExecuteEvent( myConnection , this , bytSend.Length , iCount );

}

if( bolCancel )

{

myStream.Close();

myReq.Abort();

return null;

}

}

myStream.Close();

// 接受数据

intExecuteState = 2 ;

System.Net.HttpWebResponse myRes = null;

myRes = (System.Net.HttpWebResponse) myReq.GetResponse();

myStream = myRes.GetResponseStream();

System.IO.MemoryStream myBuf = new System.IO.MemoryStream(1024);

byte[] bytBuf = new byte[1024];

while(true)

{

int iLen = myStream.Read(bytBuf,0,1024);

if(iLen ==0)

break;

myBuf.Write(bytBuf,0,iLen);

if( ExecuteEvent != null)

{

ExecuteEvent( myConnection , this , myRes.ContentLength , myBuf.Length );

}

if( bolCancel )

{

myStream.Close();

myRes.Close();

myReq.Abort();

return null ;

}

}

myStream.Close();

myRes.Close();

myReq.Abort();

byte[] bytReturn = myBuf.ToArray();

myBuf.Close();

return bytReturn ;

}

///

/// 执行命令

///

///

private bool ExecuteCommand()

{

myXMLDoc.LoadXml("");

this.ToXML( myXMLDoc.DocumentElement);

string strSend = myXMLDoc.DocumentElement.OuterXml ;

byte[] bytSend = myConnection.SendEncod.GetBytes( strSend );

byte[] bytReturn = SendByteData(bytSend);

if( bytReturn == null)

return false;

// 解析数据

intExecuteState = 3 ;

char[] chrReturn = myConnection.ReserveEncod.GetChars(bytReturn);

string strReserve =new string(chrReturn);

strReserve = strReserve.Trim();

myXMLDoc.PreserveWhitespace = true;

//try {

myXMLDoc.LoadXml( strReserve );

//}catch( Exception ext){System.Console.WriteLine( ext.ToString());}

intExecuteState = 0 ;

if( myXMLDoc.DocumentElement.GetAttribute("error")=="1")

{

throw new System.Exception("远程WEB数据库操作错误\n" + myXMLDoc.DocumentElement.InnerText );

}

return true;

}// ExecuteCommand

public string ExecuteString()

{

if( this.ExecuteCommand())

return myXMLDoc.DocumentElement.InnerText ;

else

return null;

}

#region IDbCommand 成员

public void Cancel()

{

bolCancel = true;

}

public void Prepare()

{

// TODO: 添加 XMLHttpCommand.Prepare 实现

}

///

/// 指示或指定如何解释 System.Data.IDbCommand.CommandText 属性。

///

public System.Data.CommandType CommandType

{

get{ return intCommandType ;}

set{ intCommandType = value;}

}

public System.Data.IDataReader ExecuteReader(System.Data.CommandBehavior behavior)

{

intCmdBehavior = behavior ;

intExecuteType = 0 ;

if( this.ExecuteCommand())

{

XMLHttpReader myReader = new XMLHttpReader();

myReader.FromXML( myXMLDoc.DocumentElement );

return myReader;

}

return null;

}

System.Data.IDataReader System.Data.IDbCommand.ExecuteReader()

{

intExecuteType = 0 ;

if( this.ExecuteCommand())

{

XMLHttpReader myReader = new XMLHttpReader();

myReader.FromXML( myXMLDoc.DocumentElement );

return myReader;

}

return null;

}

public object ExecuteScalar()

{

// TODO: 添加 XMLHttpCommand.ExecuteScalar 实现

return null;

}

public int ExecuteNonQuery()

{

intExecuteType = 1 ;

if( this.ExecuteCommand())

return Convert.ToInt32( myXMLDoc.DocumentElement.InnerText );

return -1;

}

///

/// 执行命令的超时时间

///

public int CommandTimeout

{

get{ return intCommandTimeout ;}

set{ intCommandTimeout = value;}

}

public System.Data.IDbDataParameter CreateParameter()

{

return (System.Data.IDbDataParameter) ( new XMLHttpParameter());

}

///

/// 数据库连接对象

///

public System.Data.IDbConnection Connection

{

get{ return myConnection ;}

set{ myConnection = (XMLHttpConnection)value;}

}

public System.Data.UpdateRowSource UpdatedRowSource

{

get{ return intUpdatedRowSource ;}

set{ intUpdatedRowSource = value;}

}

public string CommandText

{

get{ return strCommandText ;}

set{ strCommandText = value;}

}

public System.Data.IDataParameterCollection Parameters

{

get{ return myParameters ; }

}

///

/// 未支持

///

public System.Data.IDbTransaction Transaction

{

get

{

// TODO: 添加 XMLHttpCommand.Transaction getter 实现

return null;

}

set

{

// TODO: 添加 XMLHttpCommand.Transaction setter 实现

}

}

#endregion

#region IDisposable 成员

public void Dispose()

{

// TODO: 添加 XMLHttpCommand.Dispose 实现

}

#endregion

}

///

/// 通过HTTP使用XML来操作数据的数据库连接对象

///

public class XMLHttpConnection : System.Data.IDbConnection

{

private string strConnectionString = null;

private System.Data.ConnectionState intState = System.Data.ConnectionState.Closed ;

private string strDataBase = null;

private int intConnectionTimeout = 0 ;

private string strIECookies = null;

///

/// 初始化对象

///

public XMLHttpConnection( )

{

}

///

/// 初始化对象

///

///

public XMLHttpConnection( string strConn )

{

strConnectionString = strConn ;

}

///

/// 发送数据使用的编码

///

public System.Text.Encoding SendEncod = System.Text.Encoding.UTF8 ;

///

/// 接受数据使用的编码

///

public System.Text.Encoding ReserveEncod = System.Text.Encoding.GetEncoding(936);

private System.Net.Cookie mySessionCookie = null;

///

/// 正在执行命令事件处理

///

public XMLHttpDBExecutingHandler ExecuteEvent = null;

private string strHttpMethod = "POST";

///

/// 设置,返回IE浏览器使用的COOKIE字符串

///

public string IECookies

{

get{ return strIECookies ;}

set{ strIECookies = value;}

}

///

/// 向HTTP服务器提交数据的方法

///

public string HttpMethod

{

get{ return strHttpMethod ;}

set{ strHttpMethod = value;}

}

///

/// 创建一个HTTP服务器连接对象

///

///

internal System.Net.HttpWebRequest CreateHttpRequest()

{

System.Net.HttpWebRequest myReq =(System.Net.HttpWebRequest) System.Net.WebRequest.Create(strConnectionString);

// 添加用于标示用户的COOKIE对象

if( mySessionCookie != null)

{

myReq.CookieContainer = new System.Net.CookieContainer();

myReq.CookieContainer.Add( mySessionCookie );

}

myReq.ContentType = "application/x-www-form-urlencoded";

myReq.Method = strHttpMethod ;

return myReq ;

}

#region IDbConnection 成员

///

/// 更换数据库

///

///

public void ChangeDatabase(string databaseName)

{

strDataBase = databaseName ;

}

///

/// 未支持

///

///

///

public System.Data.IDbTransaction BeginTransaction(System.Data.IsolationLevel il)

{

return null;

}

///

/// 未支持

///

///

System.Data.IDbTransaction System.Data.IDbConnection.BeginTransaction()

{

return null;

}

///

/// 返回数据库连接状态

///

public System.Data.ConnectionState State

{

get{return intState ;}

}

///

/// 数据库连接字符串

///

public string ConnectionString

{

get{ return strConnectionString ; }

set{ strConnectionString = value ; intState = System.Data.ConnectionState.Closed ;}

}

///

/// 创建一个数据库命令对象

///

///

public System.Data.IDbCommand CreateCommand()

{

XMLHttpCommand NewCmd = new XMLHttpCommand();

NewCmd.Connection = this ;

NewCmd.ExecuteEvent = this.ExecuteEvent ;

NewCmd.HttpMethod = this.HttpMethod ;

return NewCmd ;

}

///

/// 打开数据库连接,本函数用于测试服务器是否可用,并设置正确的输入输出编码格式

///

public void Open()

{

intState = System.Data.ConnectionState.Connecting ;

//CustomBasic customBasicModule = new CustomBasic();

// Unregister the standard Basic authentication module.

AuthenticationManager.Unregister("Basic");

// Register the custom Basic authentication module.

//AuthenticationManager.Register(customBasicModule);

// 试图获得服务的标志用户身份的COOKIE对象

mySessionCookie = null;

if( strIECookies != null && strIECookies.Length 0 )

{

string[] strItems = StringCommon.AnalyseStringList( strIECookies , ';' , '=' , false);

if( strItems != null)

{

string strName = null;

string strValue = null;

// 根据传入的IE的Cookie值来判断用于标志用户的Cookie值

// 此处认为标志用户的COOKIE的名称中含有 SESSION

for(int iCount = 0 ; iCount = 0 )

{

strName = strItems[iCount].Trim();

strValue = strItems[iCount+1] ;

break;

}

}

if( strName != null)

{

// 向服务器发送请求,获得标示用户的Cookie对象

System.Net.HttpWebRequest myReq = this.CreateHttpRequest();

myReq.CookieContainer = new System.Net.CookieContainer();

System.IO.Stream myStream = myReq.GetRequestStream();

byte[] bytSend = System.Text.Encoding.ASCII.GetBytes("");

myStream.Write( bytSend,0 , bytSend.Length );

myStream.Close();

System.Net.HttpWebResponse myRes = myReq.GetResponse() as System.Net.HttpWebResponse ;

for(int iCount = 0 ;iCount ");

myXMLDoc.DocumentElement.SetAttribute("text", c_TestCommand );

myXMLDoc.DocumentElement.SetAttribute("data", c_TestOKFlag );

string strSend = myXMLDoc.DocumentElement.OuterXml ;

using(XMLHttpCommand myCmd = (XMLHttpCommand) this.CreateCommand())

{

for(int iCount1 = 0 ; iCount1 = 0 )

{

this.SendEncod = (System.Text.Encoding) myEncods[iCount1];

this.ReserveEncod = ( System.Text.Encoding ) myEncods[iCount2];

intState = System.Data.ConnectionState.Open ;

return ;

}

}

}

}

intState = System.Data.ConnectionState.Closed ;

}// void Open()

public void Close()

{

// TODO: 添加 XMLHttpConnection.Close 实现

}

public string Database

{

get

{

return strDataBase;

}

}

public int ConnectionTimeout

{

get

{

return intConnectionTimeout ;

}

}

#endregion

#region IDisposable 成员

public void Dispose()

{

// TODO: 添加 XMLHttpConnection.Dispose 实现

AuthenticationManager.Unregister("Basic");

}

#endregion

}

//

//

// // The CustomBasic class creates a custom Basic authentication by implementing the

// // IAuthenticationModule interface. It performs the following

// // tasks:

// // 1) Defines and initializes the required properties.

// // 2) Implements the Authenticate method.

//

// internal class CustomBasic : IAuthenticationModule

// {

//

// private string m_authenticationType ;

// private bool m_canPreAuthenticate ;

//

// // The CustomBasic constructor initializes the properties of the customized

// // authentication.

// public CustomBasic()

// {

// m_authenticationType = "Basic";

// m_canPreAuthenticate = false;

// }

//

// // Define the authentication type. This type is then used to identify this

// // custom authentication module. The default is set to Basic.

// public string AuthenticationType

// {

// get

// {

// return m_authenticationType;

// }

// }

//

// // Define the pre-authentication capabilities for the module. The default is set

// // to false.

// public bool CanPreAuthenticate

// {

// get

// {

// return m_canPreAuthenticate;

// }

// }

//

// // The checkChallenge method checks whether the challenge sent by the HttpWebRequest

// // contains the correct type (Basic) and the correct domain name.

// // Note: The challenge is in the form BASIC REALM="DOMAINNAME";

// // the Internet Web site must reside on a server whose

// // domain name is equal to DOMAINNAME.

// public bool checkChallenge(string Challenge, string domain)

// {

// bool challengePasses = false;

//

// String tempChallenge = Challenge.ToUpper();

//

// // Verify that this is a Basic authorization request and that the requested domain

// // is correct.

// // Note: When the domain is an empty string, the following code only checks

// // whether the authorization type is Basic.

//

// if (tempChallenge.IndexOf("BASIC") != -1)

// if (domain != String.Empty)

// if (tempChallenge.IndexOf(domain.ToUpper()) != -1)

// challengePasses = true;

// else

// // The domain is not allowed and the authorization type is Basic.

// challengePasses = false;

// else

// // The domain is a blank string and the authorization type is Basic.

// challengePasses = true;

//

// return challengePasses;

// }

//

// // The PreAuthenticate method specifies whether the authentication implemented

// // by this class allows pre-authentication.

// // Even if you do not use it, this method must be implemented to obey to the rules

// // of interface implementation.

// // In this case it always returns false.

// public Authorization PreAuthenticate(WebRequest request, ICredentials credentials)

// {

// return null;

// }

//

// // Authenticate is the core method for this custom authentication.

// // When an Internet resource requests authentication, the WebRequest.GetResponse

// // method calls the AuthenticationManager.Authenticate method. This method, in

// // turn, calls the Authenticate method on each of the registered authentication

// // modules, in the order in which they were registered. When the authentication is

// // complete an Authorization object is returned to the WebRequest.

// public Authorization Authenticate(String challenge, WebRequest request, ICredentials credentials)

// {

// System.Text.Encoding ASCII = System.Text.Encoding.ASCII ;

//

// // Get the username and password from the credentials

// NetworkCredential MyCreds = credentials.GetCredential(request.RequestUri, "Basic");

//

// if (PreAuthenticate(request, credentials) == null)

// Console.WriteLine("\n Pre-authentication is not allowed.");

// else

// Console.WriteLine("\n Pre-authentication is allowed.");

//

// // Verify that the challenge satisfies the authorization requirements.

// bool challengeOk = checkChallenge(challenge, MyCreds.Domain);

//

// if (!challengeOk)

// return null;

//

// // Create the encrypted string according to the Basic authentication format as

// // follows:

// // a)Concatenate the username and password separated by colon;

// // b)Apply ASCII encoding to obtain a stream of bytes;

// // c)Apply Base64 encoding to this array of bytes to obtain the encoded

// // authorization.

// string BasicEncrypt = MyCreds.UserName + ":" + MyCreds.Password;

//

// string BasicToken = "Basic " + Convert.ToBase64String(ASCII.GetBytes(BasicEncrypt));

//

// // Create an Authorization object using the encoded authorization above.

// Authorization resourceAuthorization = new Authorization(BasicToken);

//

// // Get the Message property, which contains the authorization string that the

// // client returns to the server when accessing protected resources.

// Console.WriteLine("\n Authorization Message:{0}",resourceAuthorization.Message);

//

// // Get the Complete property, which is set to true when the authentication process

// // between the client and the server is finished.

// Console.WriteLine("\n Authorization Complete:{0}",resourceAuthorization.Complete);

//

// Console.WriteLine("\n Authorization ConnectionGroupId:{0}",resourceAuthorization.ConnectionGroupId);

//

//

// return resourceAuthorization;

// }

// }

///

/// XMLHttp数据库连接服务器端模块,在某个ASP.NET页面中使用该模块就可以向 XMLHttpConnection 提供数据了

///

public class XMLHttpServer

{

private System.Data.IDbConnection myConnection ;

///

/// 数据库连接对象

///

public System.Data.IDbConnection Connection

{

get{ return myConnection ;}

set{ myConnection = value;}

}

///

/// 执行操作

///

///

///

public string Execute(System.Xml.XmlDocument InputXMLDoc )

{

const string c_TestCommand = "[testconnection]";

const string c_TestOKFlag = "test_ok_中文测试";

const string c_NullFlag = "[NULL]";

if( InputXMLDoc.DocumentElement.Name == "null")

return "";

System.Xml.XmlDocument myOutXML = new System.Xml.XmlDocument();

myOutXML.PreserveWhitespace = true;

myOutXML.LoadXml("");

myOutXML.DocumentElement.SetAttribute("version","1.0");

try

{

string strText = InputXMLDoc.DocumentElement.GetAttribute("text");

string strType = InputXMLDoc.DocumentElement.GetAttribute("type");

System.Console.WriteLine("收到命令" + InputXMLDoc.DocumentElement.OuterXml );

if( strText == c_TestCommand )

{

if( InputXMLDoc.DocumentElement.GetAttribute("data") == c_TestOKFlag )

return c_TestOKFlag ;

else

return "错误的输入编码";

}

else

{

if( myConnection != null && myConnection.State == System.Data.ConnectionState.Open )

{

using(System.Data.IDbCommand myCmd = myConnection.CreateCommand())

{

// 设置查询命令对象

myCmd.CommandText = strText ;

foreach( System.Xml.XmlNode myNode in InputXMLDoc.DocumentElement.ChildNodes )

{

if( myNode.Name == "param" && myNode is System.Xml.XmlElement )

{

System.Data.IDbDataParameter NewParam = myCmd.CreateParameter();

string strValue = myNode.InnerText ;

if( strValue == c_NullFlag )

NewParam.Value = "";

else

NewParam.Value = strValue;

myCmd.Parameters.Add( NewParam );

}

}

if( strType == "0")

{

// 查询数据库

System.Data.IDataReader myReader = myCmd.ExecuteReader();

System.Xml.XmlElement RecordElement = null;

System.Xml.XmlElement FieldElement = null;

// 添加字段信息

for( int iCount = 0 ; iCount

/// 生成用于保存错误的字符串

///

///

///

public static string CreateErrorMessage(Exception ext)

{

System.Xml.XmlDocument myXMLDoc = new System.Xml.XmlDocument();

myXMLDoc.LoadXml("");

myXMLDoc.DocumentElement.SetAttribute("error","1");

myXMLDoc.DocumentElement.InnerText = ext.ToString();

return myXMLDoc.DocumentElement.OuterXml ;

}// CreateErrorMessage

}// class XMLHttpServer

public class StringCommon

{

///

/// 分析一个保存列表数据的字符串并将分析结果保存在一个字符串数组中

/// 该字符串格式为 项目名 值分隔字符 项目值 项目分隔字符 项目名 值分隔字符 项目值 ...

/// 例如 若值分隔字符为 ; 项目分隔字符为 = 则该输入字符串格式为 a=1;b=2;c=33

/// 则本函数将生成一个字符串数组 {a,1,b,2,c,33},数组元素为偶数个

///

/// 保存列表数据的字符串

/// 项目分隔字符串

/// 值分隔字符串

/// 分析的项目是否允许重名

/// 返回的依次保存项目名称和项目值的字符串,元素个数必为偶数个,参数不正确则返回空引用

public static string[] AnalyseStringList( string strList ,char ItemSplit ,char ValueSplit ,bool AllowSameName)

{

// 判断参数的正确性

if( strList == null || strList.Length == 0 )

return null;

System.Collections.ArrayList myList = new System.Collections.ArrayList();

string strItem = null;

string strName = null;

string strValue = null;

int index1 = 0 ;

while( index1 index1 + 1 )

{

// 获得单个项目

strItem = strList.Substring( index1 , index2 - index1 );

// 获得西项目名和项目值

int index3 = strItem.IndexOf( ValueSplit );

if( index3 0 )

{

strName = strItem.Substring(0 , index3 );

strValue = strItem.Substring(index3 + 1 );

}

else

{

strName = strItem ;

strValue = "";

}

// 注册新的项目

bool bolAdd = true;

if( AllowSameName == false)

{

foreach( NameValueItem myItem in myList)

{

if( myItem.Name == strName )

{

bolAdd = false;

break;

}

}

}

if( bolAdd)

{

NameValueItem NewItem = new NameValueItem();

NewItem.Name = strName ;

NewItem.Value = strValue ;

myList.Add( NewItem );

}

}

index1 = index2 + 1 ;

}

// 输出结果

string[] strReturn = new string[ myList.Count * 2] ;

int iCount = 0 ;

foreach(NameValueItem myItem in myList)

{

strReturn[iCount ] = myItem.Name ;

strReturn[iCount+1] = myItem.Value ;

iCount += 2 ;

}

return strReturn ;

}// string[] AnalyseStringList

///

/// 内部使用的名称和值的项目对

///

private class NameValueItem

{

public string Name ;

public string Value ;

}

///

/// 将一个字符串转换为整数

///

/// 字符串

/// 默认值

/// 转换结果

public static int ToInt32Value(string strData , int DefaultValue)

{

try

{

if(strData == null || strData.Length == 0)

return DefaultValue;

return Convert.ToInt32( strData);

// char[] myChars = strData.ToCharArray();

// int iValue = 0 ;

// int iCount = 0 ;

// bool bolNegative = false ;

// foreach( char myChar in myChars)

// {

// iValue = (int)myChar ;

// if( iValue = 48 && iValue

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有