[MS-SQL]不具自動容錯移轉的鏡像設定

以下的資料庫鏡像架構大致上就是

應用程式==data==>主體伺服器==data==>鏡像伺服器

環境

兩台一樣環境的伺服器

  • Windows Server 2003 R2
  • Microsoft SQL Server 2008 R2
  • Microsoft SQL Server Management Studio 10.0.1600.22

SQL Server服務啟動帳號設定

  1. 在windows服務(services.msc) 列表中,找到SQL Server,右鍵選內容或雙擊
  2. 後選擇登入頁,在此帳戶中這裡配置登錄用戶和密碼,這個地方建議兩台伺服器都配置一組一樣的帳號,並將此帳戶加到SQL Server的管理帳戶,後續的鏡像會用這組帳號繼續設定
  3. SQL Server Agent 啟動帳號也一樣設置

主體伺服器備份

  1. 在主體伺服器用windows帳戶登入Management Studio工具,這個動作是為了後續使用語法產生SQL檔時才不會有權限錯誤
  2. 產生資料庫建立語法
    以物件總管詳細資料選取需要鏡像的資料庫,並產生Create語法(選檔案即可產生.sql檔)
  3. 匯出每個資料庫的備份檔
    backup database [DataBaseName1] to disk='D:\dbbackup\[DataBaseName1].bak'  with init
    GO
    backup database [DataBaseName2] to disk='D:\dbbackup\[DataBaseName2].bak' with init
    GO
  4. 匯出每個資料庫的記錄檔
    BACKUP LOG [DataBaseName1] TO DISK = 'D:\dbbackup\[DataBaseName1].log'
    GO
    BACKUP LOG [DataBaseName2] TO DISK = 'D:\dbbackup\[DataBaseName2].log'
    GO

還原資料到鏡像伺服器

  1. 將上一步驟產生的檔案(含*.sql、*.bak、*.log)所在dbbackup資料夾複製到鏡像資料庫
  2. 在鏡像伺服器用windows帳戶登入Management Studio工具
  3. 建立資料庫:以Management Studio開啟建立資料庫用的sql檔並執行,即可產生與主體伺服器相同名稱的資料庫
  4. 還原資料
    restore database [DataBaseName1] from disk='D:\backupz\[DataBaseName1].bak' with replace,norecovery
    GO
    restore database [DataBaseName2] from disk='D:\backupz\[DataBaseName2].bak' with replace,norecovery
    GO
  5. 還原記錄檔
    RESTORE LOG [DataBaseName1] FROM DISK = 'D:\backupz\[DataBaseName1].log'  WITH FILE=1, NORECOVERY
    GO
    RESTORE LOG [DataBaseName2] FROM DISK = 'D:\backupz\[DataBaseName2].log' WITH FILE=1, NORECOVERY
    GO
  6. 成功後會看到建立起來的資料庫名稱後面會出現(正在還原...)的狀態

於主體伺服器啟動鏡像服務

  1. 在主體伺服器的資料庫上點右鍵,選屬性,並選擇[鏡像]頁籤。接著點選[設定安全性]鈕就會開啟[設定資料庫鏡像安全性精靈]

  2. 於見證伺服器選擇頁選擇否,不使用見證伺服器

  3. 第一次使用鏡像設定會選擇本機的鏡像服務Port與鏡像端點名稱,此處採用預設值即可

  4. 設定鏡像伺服器的連線資訊

  5. 服務帳戶不用設定

  6. 完成後就會進行設定,第一次設定時會自動產生服務端點,這時就會啟動服務的Port

  7. 完成後會詢問是否要啟動鏡像,選擇不要啟動鏡像。這時如果直接啟動如果連線網域名稱錯誤就會失敗(而且還不給設定…)

  8. 接著就可以在伺服器網路位址確認鏡像伺服器的連線資訊

    需注意的是他有個奇怪的驗證,就是預設他會把鏡像伺服器的電腦名稱帶到鏡像的位址,但他的預設格式是[電腦名稱.網域名稱],沒有網域名稱時就必須在電腦名稱後面加一個句點.,否則你就會看到下面的錯誤

  9. 啟動成功後如下圖

  10. 重覆此上述動作把所有資料庫的鏡像都啟動

[C#]MS-SQL資料庫存取函式

序言

我將C#存取MS-SQL的程式包了一層方便使用。

環境

  • VS2008
  • MS-SQL 2008

存取函式類別

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Data;

namespace MyCommon.DAO
{

    /// 
    /// MS-SQL連線物件
    /// 
    public class MSSQLBaseIO
    {
        SqlConnection sqlConn = null;
        /// 
        /// 建立連線物件
        /// 
        /// 連線字串,如Data Source=localhost;Initial Catalog=dbName;User id=Account;Password=Password;
        public MSSQLBaseIO(string connectionString)
        {
            sqlConn = new SqlConnection(connectionString);
        }
        /// 
        /// 建立連線物件
        /// 
        /// 可為localhost / domainname  / 或 IP
        /// 若Host為IP,則需為true
        /// 不設定則填0,則會用MSSQL預設port
        /// 不設定則填null,express版預設為SQLEXPRESS,正式版預設為MSSQLSERVER,從服務組態可以看到
        /// 資料庫名稱
        /// 是否使用 Windows 整合驗証方式連線,false則必需填帳號密碼
        /// 登入帳號
        /// 登入密碼
        /// 連線逾期,不設定則填0
        public MSSQLBaseIO(string host,bool isHostIP,int port,
            string instanceName, string dbName,
            bool isIntegratedSecurity,
            string id, string password,
            int timeout)
        {
            StringBuilder connectionString = new StringBuilder();
            if (isHostIP) connectionString.Append("Data Source=tcp:" + host);
            else connectionString.Append("Data Source=" + host);
            if (instanceName != null) connectionString.Append("\\" + instanceName);
            if (port != 0) connectionString.Append("," + port);
            connectionString.Append(";");
            connectionString.Append("Initial Catalog=" + dbName + ";");
            if (isIntegratedSecurity) connectionString.Append("integrated security=true;");
            if (id!=null) connectionString.Append("User id=" + id + ";");
            if (password!=null) connectionString.Append("Password=" + password + ";");
            if (timeout>0) connectionString.Append("Connect Timeout=" + timeout + ";");
            sqlConn = new SqlConnection(connectionString.ToString());
        }
        /// 
        /// 取得目前連線物件
        /// 
        /// 
        public SqlConnection getConnection()
        {
            return sqlConn;
        }

        Exception _exception = null;
        /// 
        /// 取得上次執行後的錯誤
        /// 
        /// 
        public Exception getException(){
            return _exception;
        }
        /// 
        /// 關閉連線
        /// 
        public void closeConn()
        {
            closeConn(true);
        }
        /// 
        /// 關閉連線
        /// 
        /// 是否關閉連線
        public void closeConn(bool isCloseConn){
            if (sqlConn != null && isCloseConn && sqlConn.State!=System.Data.ConnectionState.Closed)
            {
                sqlConn.Close();
            }
        }
        /// 
        /// 取得新的Transaction物件
        /// 
        /// Transaction物件
        public SqlTransaction getNewTransaction()
        {
            if (sqlConn.State != System.Data.ConnectionState.Open)
                sqlConn.Open();
            return sqlConn.BeginTransaction();
        }

        /// 
        /// 執行SQL命令,執行後會關閉SQL連線
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 變動筆數
        public int ExecuteNonQuery(string sqlcmd, bool isStoredProcedure)
        {
            return ExecuteNonQuery(sqlcmd,isStoredProcedure, true);
        }
        /// 
        /// 執行SQL命令
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 是否關閉連線
        /// 變動筆數
        public int ExecuteNonQuery(string sqlcmd, bool isStoredProcedure, bool isCloseConn)
        {
            SqlParameter[] sqlparams = null;
            return ExecuteNonQuery(sqlcmd, ref sqlparams, isStoredProcedure, isCloseConn);
        }
        /// 
        /// 執行SQL命令
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// 是否關閉連線
        /// 變動筆數
        public int ExecuteNonQuery(string sqlcmd, ref SqlParameter[] sqlparams, bool isStoredProcedure, bool isCloseConn)
        {
            return ExecuteNonQuery(sqlcmd, ref sqlparams, isStoredProcedure, null, isCloseConn);
        }
        /// 
        /// 執行SQL命令
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// SqlTransaction,可為null
        /// 是否關閉連線
        /// 變動筆數
        public int ExecuteNonQuery(string sqlcmd, ref SqlParameter[] sqlparams,bool isStoredProcedure, SqlTransaction trans, bool isCloseConn)
        {
            int result = -1;
            _exception = null;
            try
            {
                if (sqlConn.State != System.Data.ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand cmd;
                if (trans==null)
                    cmd = new SqlCommand(sqlcmd, sqlConn);
                else
                    cmd = new SqlCommand(sqlcmd, sqlConn, trans);
                if(isStoredProcedure)
                    cmd.CommandType = CommandType.StoredProcedure;
                if (sqlparams!=null)
                    cmd.Parameters.AddRange(sqlparams);
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                _exception = ex;
            }
            finally
            {
                closeConn(isCloseConn);
            }
            return result;
        }
        /// 
        /// 取得多列資料,執行後會關閉SQL連線
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 是否關閉連線
        /// 多列資料
        public List ExecuteReader(string sqlcmd, bool isStoredProcedure)
        {
            return ExecuteReader(sqlcmd, isStoredProcedure, true);
        }
        /// 
        /// 取得多列資料
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 是否關閉連線
        /// 多列資料
        public List ExecuteReader(string sqlcmd, bool isStoredProcedure, bool isCloseConn)
        {
            SqlParameter[] sqlparams = null;
            return ExecuteReader(sqlcmd, ref sqlparams, isStoredProcedure, isCloseConn);
        }
        /// 
        /// 取得多列資料
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// 是否關閉連線
        /// 多列資料
        public List ExecuteReader(string sqlcmd, ref SqlParameter[] sqlparams, bool isStoredProcedure, bool isCloseConn)
        {
            return ExecuteReader(sqlcmd, ref sqlparams, isStoredProcedure, null, isCloseConn);
        }
        /// 
        /// 取得多列資料
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// SqlTransaction,可為null
        /// 是否關閉連線
        /// 多列資料
        public List ExecuteReader(string sqlcmd, ref SqlParameter[] sqlparams, bool isStoredProcedure, SqlTransaction trans, bool isCloseConn)
        {
            List result = null;
            _exception = null;
            SqlDataReader reader = null;
            try
            {
                if (sqlConn.State != System.Data.ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand cmd;
                if (trans == null)
                    cmd = new SqlCommand(sqlcmd, sqlConn);
                else
                    cmd = new SqlCommand(sqlcmd, sqlConn, trans);
                if (isStoredProcedure)
                    cmd.CommandType = CommandType.StoredProcedure;
                if (sqlparams != null)
                    cmd.Parameters.AddRange(sqlparams);
                reader = cmd.ExecuteReader();
                object[] item;
                if (reader.HasRows)
                {
                    result = new List();
                    while (reader.Read())
                    {
                        item = new object[reader.FieldCount];
                        for (int j = 0; j < reader.FieldCount; j++)
                        {
                            item[j] = reader[j];
                        }
                        result.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                _exception = ex;
            }
            finally
            {
                if (reader!=null) reader.Close();
                closeConn(isCloseConn);
            }
            return result;
        }

        /// 
        /// 取得單一值,執行後會關閉SQL連線
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 單一值
        public object ExecuteScalar(string sqlcmd, bool isStoredProcedure)
        {
            return ExecuteScalar(sqlcmd, isStoredProcedure, true);
        }
        /// 
        /// 取得單一值
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 是否關閉連線
        /// 單一值
        public object ExecuteScalar(string sqlcmd, bool isStoredProcedure, bool isCloseConn)
        {
            SqlParameter[] sqlparams = null;
            return ExecuteScalar(sqlcmd, ref sqlparams, isStoredProcedure, isCloseConn);
        }
        /// 
        /// 取得單一值
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// 是否關閉連線
        /// 單一值
        public object ExecuteScalar(string sqlcmd,ref SqlParameter[] sqlparams, bool isStoredProcedure, bool isCloseConn)
        {
            return ExecuteScalar(sqlcmd,ref sqlparams, isStoredProcedure, null, isCloseConn);
        }
        /// 
        /// 取得單一值
        /// 
        /// SQL命令
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// SqlTransaction,可為null
        /// 是否關閉連線
        /// 單一值
        public object ExecuteScalar(string sqlcmd,ref SqlParameter[] sqlparams, bool isStoredProcedure, SqlTransaction trans, bool isCloseConn)
        {
            object result = null;
            _exception = null;
            try
            {
                if (sqlConn.State != System.Data.ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand cmd;
                if (trans == null)
                    cmd = new SqlCommand(sqlcmd, sqlConn);
                else
                    cmd = new SqlCommand(sqlcmd, sqlConn, trans);
                if (isStoredProcedure)
                    cmd.CommandType = CommandType.StoredProcedure;
                if (sqlparams != null)
                    cmd.Parameters.AddRange(sqlparams);
                result = cmd.ExecuteScalar();
              
            }
            catch (Exception ex)
            {
                _exception = ex;
            }
            finally
            {
                closeConn(isCloseConn);
            }
            return result;
        }

        /// 
        /// 取得單一列資料,執行後會關閉SQL連線
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 單一列資料
        public object[] ExecuteReaderSingleRow(string sqlcmd, bool isStoredProcedure)
        {
            return ExecuteReaderSingleRow(sqlcmd, isStoredProcedure, true);
        }
        /// 
        /// 取得單一列資料
        /// 
        /// SQL命令
        /// 是否為預存程序
        /// 是否關閉連線
        /// 單一列資料
        public object[] ExecuteReaderSingleRow(string sqlcmd, bool isStoredProcedure, bool isCloseConn)
        {
            SqlParameter[] sqlparams = null;
            return ExecuteReaderSingleRow(sqlcmd, ref sqlparams, isStoredProcedure, isCloseConn);
        }
        /// 
        /// 取得單一列資料
        /// 
        /// SQL命令,參數部份以 @參數名稱 標示
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// 是否關閉連線
        /// 單一列資料
        public object[] ExecuteReaderSingleRow(string sqlcmd,ref SqlParameter[] sqlparams, bool isStoredProcedure, bool isCloseConn)
        {
            return ExecuteReaderSingleRow(sqlcmd,ref sqlparams, isStoredProcedure, null, isCloseConn);
        }
        /// 
        /// 取得單一列資料
        /// 
        /// SQL命令,參數部份以 @參數名稱 標示
        /// 命令參數,可為null,參數名稱為 @參數名稱
        /// 是否為預存程序
        /// SqlTransaction,可為null
        /// 是否關閉連線
        /// 單一列資料
        public object[] ExecuteReaderSingleRow(string sqlcmd, ref SqlParameter[] sqlparams, bool isStoredProcedure, SqlTransaction trans, bool isCloseConn)
        {
            object[] result = null;
            _exception = null;
            SqlDataReader reader = null;
            try
            {
                if (sqlConn.State != System.Data.ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand cmd;
                if (trans == null)
                    cmd = new SqlCommand(sqlcmd, sqlConn);
                else
                    cmd = new SqlCommand(sqlcmd, sqlConn, trans);
                if (isStoredProcedure)
                    cmd.CommandType = CommandType.StoredProcedure;
                if (sqlparams != null)
                    cmd.Parameters.AddRange(sqlparams);
                reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
                object[] item;
                if (reader.HasRows)
                {
                    reader.Read();
                    item = new object[reader.FieldCount];
                    for (int j = 0; j < reader.FieldCount; j++)
                    {
                        item[j] = reader[j];
                    }
                    result = item;
                }
            }
            catch (Exception ex)
            {
                _exception = ex;
            }
            finally
            {
                if (reader != null) reader.Close();
                closeConn(isCloseConn);
            }
            return result;
        }
    }
}
 

[C++]Oracle資料庫存取函式(OCCI)


開發環境

  • VS2008
  • Oracle 10g Express Edition

下載OCCI函式庫

  1. 下載occivc9win32-111060-132181.zip,解壓縮到自行建立的lib目錄後可以得到目錄
  2. 下載instantclient-sdk-win32-11.2.0.1.0.zip,解壓縮後可在[instantclient_11_2\sdk\]目錄找到[include]
  3. 將上述兩個目錄複製到某個目錄下,如[D:\Project\CPPProject\occivc9win32-111060-132181]

設定開發專案

  1. 將[lib]目錄中的oraocci11.dll與oraocci11d.dll都複製到[C:\WINDOWS\system32]
  2. 於開發用的C++專案設定屬性:展開[組態屬性→C/C++→一般],修改[其他Include目錄],指到[include]目錄
  3. 展開[組態屬性→連結器→一般],修改[其他程式庫目錄],指到lib目錄
  4. 展開[組態屬性→連結器→輸入],修改[其他相依性],,增加[oraocci11d.lib]

資料庫連線函式庫

  1. OracleIO.h
    #pragma once
    #include
    namespace commonio {
     class OracleIO
     {
      private:
       oracle::occi::Environment *env;
       oracle::occi::Connection *conn;
       oracle::occi::Statement *stmt;
       oracle::occi::ResultSet *rs;
       const char* _ip;
       unsigned int _port;
       const char* _account;
       const char* _pswd;
       const char* _defaultDB;
      public:
       OracleIO(const char* ip,unsigned int port,const char* account,const char* pswd,const char* defaultDB);
       ~OracleIO(void);
    
       
       /**
       **取得連線
       **/
       oracle::occi::Connection *getConn();
       void closeConn();
       void closeStatement();
       /**
       **執行查詢
       **/
       oracle::occi::ResultSet *executeQuery(const char* sqlStr);
       /**
       **執行新增、更新、刪除
       **/
       unsigned int executeUpdate(const char* sqlStr);
     };
    };
  2. OracleIO.cpp
    #include "StdAfx.h"
    #include "OracleIO.h"
    #include "CommonFunc.h"
    #include 
    using namespace std;
    using namespace commonio;
    using namespace oracle::occi;
    OracleIO::OracleIO(const char* ip,unsigned int port,const char* account,const char* pswd,const char* defaultDB)
    {
     _ip=ip;
     _port=port;
     _account=account;
     _pswd=pswd;
     _defaultDB=defaultDB;
     env = oracle::occi::Environment::createEnvironment("UTF8","UTF8",Environment::THREADED_MUTEXED);
     conn=NULL;
     stmt=NULL;
     rs=NULL;
    }
    
    
    
    OracleIO::~OracleIO(void)
    {
     closeConn();
     oracle::occi::Environment::terminateEnvironment(env);
    }
    
    Connection* OracleIO::getConn(){
     if(conn==NULL){
      string port;
      commonio::CommonFunc::itostr(_port,port,10);
      string connectString="//";
      connectString=connectString + _ip+ ":" + port + "/" + _defaultDB;
      conn = env->createConnection(_account, _pswd, connectString);
     }
     return conn;
    }
    void OracleIO::closeConn(){
     if(conn!=NULL){
      closeStatement();
      env->terminateConnection(conn);
      conn=NULL;
     }
    }
    void OracleIO::closeStatement(){
     if(stmt!=NULL){
      if(rs!=NULL){
       stmt->closeResultSet(rs);
       rs=NULL;
      }
      conn->terminateStatement(stmt);
      stmt=NULL;
     }
    }
    
    ResultSet *OracleIO::executeQuery(const char* sqlStr){
     try{
      getConn();
      string sql=string(sqlStr);
      stmt = conn->createStatement(sql);
      rs = stmt->executeQuery();  
     }catch (SQLException &sqlExcp){
      cerr <createStatement(sql);
      result=stmt->executeUpdate();
      closeStatement();
     }catch (SQLException &sqlExcp){
      cerr <
  3. 額外函式
    • CommonFunc.h
      #pragma once
      using namespace std;
      namespace commonio {
       class CommonFunc
       {
       public:
        static void itostr(int value, std::string& buf, int base);
       };
      };
    • CommonFunc.cpp
      #include "stdafx.h"
      #include 
      #include "CommonFunc.h"
      using namespace std;
      using namespace commonio;
      
      void CommonFunc::itostr(int value, std::string& buf, int base){
       
       int i = 30;
       
       buf = "";
       
       for(; value && i ; --i, value /= base) buf = "0123456789abcdef"[value % base] + buf;
       
      }

使用範例

#include "stdafx.h"
#include 
#include 
#include "OracleIO.h"
using namespace std;
using namespace commonio;

int main(int argc, _TCHAR* argv[])
{

 OracleIO oracleio=OracleIO("localhost",1521,"oracle","oracle","XE");
 /**
 **更新測試Start
 **/ 
 unsigned int changeNum= oracleio.executeUpdate("update test1 set testname='test123' where testsn=1");
 cout<<"changeNum:"< listOfColumns =rs->getColumnListMetaData();
 for(int i=0;inext())
 {
  for(int i=0;igetString(i+1)<<"\t";
  }
  cout <

[C++]MySQL資料庫存取函式(MySQL++)

序言

此篇為研究C++存取MySQL所寫的函式。

環境

編譯MySQL++函式庫

  1. 下載mysql++-3.1.0.tar.gz,解壓縮後可以得到[mysql++-3.1.0]目錄
  2. 進入[mysql++-3.1.0\vc2008\]目錄會找到[mysql++_mysqlpp.vcproj]專案,以Visual Studio 2008開啟
  3. 編譯時MySQL++會需要MySQL的版本定義標頭檔,函式庫預設MySQL安裝於[C:\Program Files\MySQL\MySQL Server 5.1],所以需先確認是否有[C:\Program Files\MySQL\MySQL Server 5.1\include]與[C:\Program Files\MySQL\MySQL Server 5.1\lib\opt]是否存在,如果MySQL放在其他目錄,可透過專案屬性修改
    • 開啟專案屬性
    • 展開[組態屬性→C/C++→一般],修改[其他Include目錄],指到MySQL的安裝目錄中的include資料夾
    • 展開[組態屬性→連結器→一般],修改[其他程式庫目錄],指到MySQL的安裝目錄中的lib\opt資料夾
  4. 建置專案
  5. 建置成功後會在[mysql++-3.1.0\vc2008\Debug]目錄下看到編譯完成的mysqlpp_d.dll、mysqlpp_d.lib

設定開發專案

  1. 將上一步產生的[mysqlpp_d.dll]複製到[C:\WINDOWS\system32]
  2. 於開發用的C++專案設定屬性:展開[組態屬性→C/C++→一般],修改[其他Include目錄],指到MySQL的安裝目錄中的include資料夾與mysql++函式庫的lib目錄
  3. 展開[組態屬性→連結器→一般],修改[其他程式庫目錄],指到MySQL的安裝目錄中的lib\opt資料夾與mysql++編譯後的Debug目錄
  4. 展開[組態屬性→連結器→輸入],修改[其他相依性],,增加[mysqlpp_d.lib]與 [libmysql.lib]

資料庫連線函式庫

  1. MySQLIO.h
    #pragma once
    
    #include 
    
    namespace commonio {
    
         class MySQLIO
    
         {
    
         private:
    
               mysqlpp::Connection _conn;
    
               const char* _ip;
    
               unsigned int _port;
    
               const char* _account;
    
               const char* _pswd;
    
               const char* _defaultDB;
    
               const char* _error;
    
               int _errnum;
    
         public:
    
               /**
    
               **MySQL資料庫連線物件
    
               **/
    
               MySQLIO(const char* ip,unsigned int port,const char* account,const char* pswd,const char* defaultDB);
    
               ~MySQLIO(void);
    
               /**
    
               **錯誤訊息
    
               **/
    
               const char* error();
    
               int errnum();
    
               /**
    
               **取得連線
    
               **/
    
               mysqlpp::Connection getConn(){return _conn;}
    
               /**
    
               **執行查詢
    
               **/
    
               mysqlpp::StoreQueryResult executeQuery(const char* sqlStr);
    
               /**
    
               **執行查詢
    
               **/
    
               mysqlpp::StoreQueryResult executeQuery(const char* sqlStr,mysqlpp::SQLQueryParms & param);
    
               /**
    
               **執行新增、更新、刪除
    
               **/
    
               mysqlpp::SimpleResult executeUpdate(const char* sqlStr);
    
               /**
    
               **執行新增、更新、刪除
    
               **/
    
               mysqlpp::SimpleResult executeUpdate(const char* sqlStr,mysqlpp::SQLQueryParms & param);
    
         };
    
    };
  2. MySQLIO.cpp
    #include "stdafx.h"
    
    #include "MySQLIO.h"
    
     
    
    using namespace std;
    
    using namespace commonio;
    
    MySQLIO::MySQLIO(const char* ip,unsigned int port,const char* account,const char* pswd,const char* defaultDB):_conn(false)
    
    {
    
         _ip=ip;
    
         _port=port;
    
         _account=account;
    
         _pswd=pswd;
    
         _defaultDB=defaultDB;
    
         _conn.set_option(new mysqlpp::SetCharsetNameOption("utf8") );
    
    }
    
    const char* MySQLIO::error(){return _error;}
    
    int MySQLIO::errnum(){return _errnum;}
    
    mysqlpp::StoreQueryResult MySQLIO::executeQuery(const char* sqlStr){
    
         _error=NULL;
    
         _errnum=0;
    
         
    
         try{
    
               if(!_conn.connected()) {
    
                    if(!_conn.connect(_defaultDB, _ip,
    
                         _account, _pswd,_port)){
    
                               _error=_conn.error();
    
                               _errnum=_conn.errnum();
    
                               
    
                    }
    
               }
    
               if(_conn.connected()) {
    
                    mysqlpp::Query query = _conn.query(sqlStr);
    
                    if (mysqlpp::StoreQueryResult res = query.store()) {
    
                         query.reset();
    
                         return res;
    
                    }else{
    
                         _error=query.error();
    
                         _errnum=query.errnum();
    
                         
    
                    }
    
               }
    
         }catch(const mysqlpp::ConnectionFailed &e){
    
               _error=e.what();
    
               _errnum=e.errnum();
    
         }catch(const mysqlpp::BadQuery &e){        
    
               _error=e.what();
    
               _errnum=e.errnum();
    
         }catch (const mysqlpp::BadConversion & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadFieldName & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadIndex & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadInsertPolicy & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadOption & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadParamCount & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::Exception& e) {
    
               _error=e.what();
    
         }
    
         return mysqlpp::StoreQueryResult();
    
     
    
    }
    
    mysqlpp::StoreQueryResult MySQLIO::executeQuery(const char* sqlStr,mysqlpp::SQLQueryParms & param){
    
         _error=NULL;
    
         _errnum=0;
    
         
    
         try{
    
               if(!_conn.connected()) {
    
                    if(!_conn.connect(_defaultDB, _ip,
    
                         _account, _pswd,_port)){
    
                               _error=_conn.error();
    
                               _errnum=_conn.errnum();
    
                               
    
                    }
    
               }
    
               if(_conn.connected()) {
    
                    mysqlpp::Query query = _conn.query(sqlStr);
    
                    query.parse();
    
                    if (mysqlpp::StoreQueryResult res = query.store(param)) {
    
                         query.reset();
    
                         return res;
    
                    }else{
    
                         _error=query.error();
    
                         _errnum=query.errnum();
    
                         
    
                    }
    
               }
    
         }catch(const mysqlpp::ConnectionFailed &e){
    
               _error=e.what();
    
               _errnum=e.errnum();
    
         }catch(const mysqlpp::BadQuery &e){        
    
               _error=e.what();
    
               _errnum=e.errnum();
    
         }catch (const mysqlpp::BadConversion & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadFieldName & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadIndex & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadInsertPolicy & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadOption & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadParamCount & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::Exception& e) {
    
               _error=e.what();
    
         }
    
         return mysqlpp::StoreQueryResult();
    
    }
    
     
    
     
    
    mysqlpp::SimpleResult MySQLIO::executeUpdate(const char* sqlStr){
    
         _error=NULL;
    
         _errnum=0;
    
         
    
         try{
    
               if(!_conn.connected()) {
    
                    if(!_conn.connect(_defaultDB, _ip,
    
                         _account, _pswd,_port)){
    
                               _error=_conn.error();
    
                               _errnum=_conn.errnum();
    
                    }
    
               }
    
               if(_conn.connected()) {
    
                    mysqlpp::Query query = _conn.query(sqlStr);
    
                    query.parse();
    
                    if (mysqlpp::SimpleResult res = query.execute()) {
    
                         query.reset();
    
                         return res;
    
                    }else{
    
                         _error=query.error();
    
                         _errnum=query.errnum();
    
                         
    
                    }
    
               }
    
         }catch(const mysqlpp::ConnectionFailed &e){
    
               _error=e.what();
    
               _errnum=e.errnum();
    
         }catch(const mysqlpp::BadQuery &e){        
    
               _error=e.what();
    
               _errnum=e.errnum();
    
         }catch (const mysqlpp::BadConversion & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadFieldName & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadIndex & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadInsertPolicy & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadOption & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadParamCount & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::Exception& e) {
    
               _error=e.what();
    
         }
    
         return mysqlpp::SimpleResult();
    
    }
    
     
    
    mysqlpp::SimpleResult MySQLIO::executeUpdate(const char* sqlStr,mysqlpp::SQLQueryParms & param){
    
         _error=NULL;
    
         _errnum=0;
    
         
    
         try{
    
               if(!_conn.connected()) {
    
                    if(!_conn.connect(_defaultDB, _ip,
    
                         _account, _pswd,_port)){
    
                               _error=_conn.error();
    
                               _errnum=_conn.errnum();
    
                    }
    
               }
    
               if(_conn.connected()) {
    
                    mysqlpp::Query query = _conn.query(sqlStr);
    
                    query.parse();
    
                    if (mysqlpp::SimpleResult res = query.execute(param)) {
    
                         query.reset();
    
                         return res;
    
                    }else{
    
                         _error=query.error();
    
                         _errnum=query.errnum();
    
                         
    
                    }
    
               }
    
         }catch(const mysqlpp::ConnectionFailed &e){
    
               _error=e.what();
    
               _errnum=e.errnum();
    
         }catch(const mysqlpp::BadQuery &e){        
    
               _error=e.what();
    
               _errnum=e.errnum();
    
         }catch (const mysqlpp::BadConversion & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadFieldName & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadIndex & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadInsertPolicy & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadOption & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::BadParamCount & e) {
    
               _error=e.what();
    
         }catch (const mysqlpp::Exception& e) {
    
               _error=e.what();
    
         }
    
         return mysqlpp::SimpleResult();
    
    }
    
     
    
    MySQLIO::~MySQLIO(void)
    
    {    
    
    }

使用範例

#include "stdafx.h"

#include 

#include "MySQLIO.h"

using namespace std;

using namespace commonio;

 

int main(int argc, _TCHAR* argv[])

{

     string data;

 

     MySQLIO mysqlio("localhost",3306,"root",NULL,"pklogdb");

     mysqlpp::SQLQueryParms params;

           params << 2;

     if (mysqlpp::StoreQueryResult res = mysqlio.executeQuery("select * from gametypetab where gameType=%0q",params)) {

           mysqlpp::StoreQueryResult::const_iterator it;

           mysqlpp::Row::const_iterator colit;

           for (it = res.begin(); it != res.end(); ++it) {

                mysqlpp::Row row = *it;

                cout << '\t';

                for (colit = row.begin(); colit != row.end(); ++colit) {

                     if(colit->is_null()){

                           cout << "\t (NULL)"<< endl;

                     }else{

                           string col(*colit);

                           cout << '\t' << col << endl;                         

                     }

                }

                cout << endl;

           }

           params.clear();

           string value("TEST");

           params<0){

                cerr << "success update"<< endl;

           }

     }else{

           cerr << "Failed to get item list: "<< endl;

           return 1;

     }

 

    return 0;

}

[C++]Http Get與Post函式(Curl)

序言

此篇記錄我使用Curl函式庫來達成Http的Get與Post方法。

開發環境

編譯Curl函式庫

  1. 下載curl-7.21.4.zip,解壓縮後可以得到[curl-7.21.4]目錄
  2. 進入[curl-7.21.4]目錄會找到[vc6curl.dsw]專案,以Visual Studio 2008開啟
  3. 建置專案
  4. 建置成功後會在[curl-7.21.4\lib\DLL-Debug]目錄下看到編譯完成的libcurld.dll、libcurld_imp.lib

設定開發專案

  1. 將上一步產生的[libcurld.dll]複製到[C:\WINDOWS\system32]
  2. 於開發用的C++專案設定屬性:展開[組態屬性→C/C++→一般],修改[其他Include目錄],指到[curl-7.21.4]目錄中的include資料夾
  3. 展開[組態屬性→連結器→一般],修改[其他程式庫目錄],指到[curl-7.21.4\lib\DLL-Debug]目錄
  4. 展開[組態屬性→連結器→輸入],修改[其他相依性],,增加[libcurld_imp.lib]


Http連線函式庫

  1. HttpIO.h
    #pragma once
    #include 
    using namespace std;
    namespace commonio {
     class HttpIO
     {
     public:
      /**
      **post方法
      **isRedirect:是否依head redirect
      **param:傳參數,如val1=123&val2=abc
      **timeout:等待回應逾期時間(秒),為不限制
      **/
      static string doPost(string URL,bool isRedirect,string param="",long timeout=0);
      /**
      **get方法
      **isRedirect:是否依head redirect
      **timeout:等待回應逾期時間(秒),為不限制
      **/
      static string doGet(string URL,bool isRedirect,long timeout=0);
     };
    };
  2. HttpIO.cpp
    #include "StdAfx.h"
    #include 
    //#include 
    #include 
    #include "HttpIO.h"
    
    using namespace std;
    using namespace commonio;
    
    
    // This is the writer call back function used by curl
    size_t writer (char *data, size_t size, size_t nmemb, std::string *buffer)
    {
        size_t result = 0;
        if (buffer != NULL)
        {
            buffer->append (data, size * nmemb);
            result = size * nmemb;
        }
        return result;
    }    
    
    
    
    string HttpIO::doPost(string URL,bool isRedirect,string param,long timeout)
    {
        CURL *curl;
        CURLcode cc;
        string bufferdata;
     string error;
     //string user_agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
     if ((curl = curl_easy_init()) == NULL)
            exit(1);
     curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
        curl_easy_setopt(curl, CURLOPT_URL, URL.c_str()); // URL used for posting
     //curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent);
     
    
     if(isRedirect) curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //Redirect
     if(timeout>0) curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); //times out after second
        curl_easy_setopt(curl, CURLOPT_POST, 1);//Tell libcurl to use POST
     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, param.c_str());
        curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION,   writer);
        curl_easy_setopt (curl, CURLOPT_WRITEDATA,&bufferdata);
        //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  //Debug
        //cc=curl_easy_setopt(curl, CURLOPT_HEADER, 1);    //Debug
     cc = curl_easy_perform(curl);
     
        curl_easy_cleanup(curl);
        if(cc == CURLE_OK)
            {
                string data;
                data=bufferdata;
                bufferdata="";
       return data;
            }      
            else
            {
                //cout << "Error: [" << cc << "] - "<< error;
       string data("");
       return data;
            }
    }
    
    string HttpIO::doGet(string URL,bool isRedirect,long timeout)
    {
        CURL *curl;
        CURLcode cc;
    
        string bufferdata;
     string error;
        if ((curl = curl_easy_init()) == NULL)
            exit(1);
     curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
        curl_easy_setopt(curl, CURLOPT_URL, URL.c_str()); // URL used for posting
     if(isRedirect) curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //Redirect
     if(timeout>0) curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); //times out after second
        curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION,   writer);
        curl_easy_setopt (curl, CURLOPT_WRITEDATA,&bufferdata);
     //cc=curl_easy_setopt(curl, CURLOPT_HEADER, 1);    //Debug
        cc = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        if(cc == CURLE_OK)
            {
                string data;
                data=bufferdata;
                bufferdata="";
       return data;
            }      
            else
            {
                //cout << "Error: [" << cc << "] - "<< error;
       string data("");
       return data;
            }
    }
  3. 額外函式
    • CommonFunc.h
      #pragma once
      using namespace std;
      namespace commonio {
       class CommonFunc
       {
       public:
        static void str_split(const string& str,
              vector& tokens,
              const string& delimiters = " ");
        static vector> paramToArray(string param);
       };
      };
    • CommonFunc.cpp
      #include "stdafx.h"
      
      #include 
      #include 
      #include "CommonFunc.h"
      using namespace std;
      using namespace commonio;
      void CommonFunc::str_split(const string& str,
                            vector& tokens,
                            const string& delimiters){
          // Skip delimiters at beginning.
          string::size_type lastPos = str.find_first_not_of(delimiters, 0);
          // Find first "non-delimiter".
          string::size_type pos     = str.find_first_of(delimiters, lastPos);
      
          while (string::npos != pos || string::npos != lastPos)
          {
              // Found a token, add it to the vector.
              tokens.push_back(str.substr(lastPos, pos - lastPos));
              // Skip delimiters.  Note the "not_of"
              lastPos = str.find_first_not_of(delimiters, pos);
              // Find next "non-delimiter"
              pos = str.find_first_of(delimiters, lastPos);
          }
      }
      
      
      vector> CommonFunc::paramToArray(string param){
         vector>  arr;
         vector p;
         CommonFunc::str_split(param,p,"&");
         if (param.find("&")>-1) {
          vector p2;
          int j = 0;
          for (unsigned int i = 0; i < p.size(); i++) {
           if (p[i].find_first_of("amp;")==0) {
            p2[j - 1]=p2[j - 1] + "&" + p[i].substr(4);
            j--;
           }
           p2.push_back(p[i]);
           j++;
          }
          p=p2;
         }
      
         for (unsigned int i = 0; i < p.size(); i++) {
          vector item;
          CommonFunc::str_split(p[i],item,"=");
          if (item.size() == 2) {
           arr.push_back(item);
          }
         }
         return arr;
        }

使用範例程式

#include "stdafx.h"
#include 
#include 
#include "HttpIO.h"
using namespace std;
using namespace commonio;

int main(int argc, _TCHAR* argv[])
{
 string data=HttpIO::doGet("http://www.google.com",false);
 cout << '\t' << data << endl;
    return 0;
}

[Java]Http Get與Post函式2(HttpURLConnection)

序言

之前我發的一篇[Http Get與Post函式]中已經實現了可維持Session狀態的HTTP連線函式,但我發現了Apache提供的這個Client在Windows下會有連線數上限的問題(聽說好像調整XP連線數可以解決)。
另外,有的時候使用HTTP連線也不一定需要維持Session狀態,這時候其實就不需要另外加入Apache的http client的JAR。
因此我還是整理一下如果一個不維持Session狀態的HTTP Client連線怎麼達成。

函式原始碼

參數轉換函式
package common.control;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;

public class HTTPParseFunc {

/**
* hashMapToString
*
* @param map
* @param charset編碼
* ,如HTTP.UTF_8
* @return
* @throws UnsupportedEncodingException
*/
@SuppressWarnings("unchecked")
public static String hashMapToString(HashMap<String, String> map,
String charset) throws UnsupportedEncodingException {
StringBuffer result = new StringBuffer();
java.util.Iterator it = map.entrySet().iterator();
boolean isfirst = true;
while (it.hasNext()) {
java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
if (isfirst) {
isfirst = false;
} else {
result.append("&");
}
result
.append(URLEncoder.encode(entry.getKey().toString(),
charset));
result.append("=");
result.append(URLEncoder.encode(entry.getValue().toString(),
charset));
}
return result.toString();
}
/**
* 將inputStream轉為String
*
* @param is
* inputStream
* @param charset
* 編碼,如HTTP.UTF_8
* @return inputStream的內容
* @throws UnsupportedEncodingException
*/
public static String inputStream2String(InputStream is, String charset)
throws UnsupportedEncodingException {
BufferedReader in = new BufferedReader(new InputStreamReader(is,
charset));
StringBuffer buffer = new StringBuffer();
String line = "";
try {
boolean isfirst = true;
while ((line = in.readLine()) != null) {
if (!isfirst) {
buffer.append("\n");
} else {
isfirst = false;
}
buffer.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}

return buffer.toString();
}

/**
* HTTP 傳輸參數分割
* @param param 如name1=value1&name2=value2
* @return
* @throws UnsupportedEncodingException
*/
public static ArrayList<String[]> paramToArray(String param)
throws UnsupportedEncodingException {
ArrayList<String[]> arr = null;
String[] p = param.split("&");
if (param.toLowerCase().contains("&amp;")) {
ArrayList<String> p2 = new ArrayList<String>();
int j = 0;
for (int i = 0; i < p.length; i++) {
if (p[i].toLowerCase().startsWith("amp;")) {
p2.set(j - 1, p2.get(j - 1) + "&amp;" + p[i].substring(4));
j--;
}
p2.add(p[i]);
j++;
}
p2.toArray(p);
}

for (int i = 0; i < p.length; i++) {
String[] item = p[i].split("=");
if (item.length == 2) {
if (arr == null)
arr = new ArrayList<String[]>();
// item[0]=URLDecoder.decode(item[0],charset);
// item[1]=URLDecoder.decode(item[1],charset);
arr.add(item);
}
}
return arr;
}
}

連線參數模型
package common.model;

public class HTTPResponse {
String html=null;
Integer statusCode=null;
public String getHtml() {
return html;
}
public void setHtml(String html) {
this.html = html;
}
public Integer getStatusCode() {
return statusCode;
}
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
}


HTTP連線函式
package common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.HashMap;

import common.control.HTTPParseFunc;
import common.model.HTTPResponse;



public class HTTPBaseIO2 {
public enum Method {
get, post
}
/**
* 送出Request
* @param urlpath URL
* @param method HTTPBaseIO2.Method.get/post
* @param params HashMap<Name, Value>
* @param charset UTF-8
* @param timeout 連線逾時null:不設限/millisecond
* @return 回傳的html
* @throws UnsupportedEncodingException
*/
public static HTTPResponse doSend(String urlpath, Method method,
HashMap<String, String> params, String charset,boolean isAutoRedirect, Integer timeout,Proxy proxy) throws UnsupportedEncodingException {
HTTPResponse result=new HTTPResponse();
String param=null;
if(params!=null){
param=HTTPParseFunc.hashMapToString(params, charset);
}
if(method==Method.post){
result=sendPost(urlpath,param,charset,isAutoRedirect,timeout,proxy);
}else{
result=sendGet(urlpath,param,charset,isAutoRedirect,timeout,proxy);
}
return result;
}
/**
* 向指定URL發送GET方法的請求
*
* @param url
* 發送請求的URL
* @param param
* 請求參數,請求參數應該是name1=value1&name2=value2的形式。
* @return URL所代表遠程資源的響應
*/
public static HTTPResponse sendGet(String url, String params, String charset,boolean isAutoRedirect, Integer timeout,Proxy proxy) {
HTTPResponse result=new HTTPResponse();
HttpURLConnection conn=null;
BufferedReader in = null;
try {
String urlName = url;
if(params!=null) urlName+= "?" + params;
URL realUrl = new URL(urlName);

HttpURLConnection.setFollowRedirects(isAutoRedirect);
// 打開和URL之間的連接
if(proxy!=null)
conn = (HttpURLConnection)realUrl.openConnection(proxy);
else
conn = (HttpURLConnection)realUrl.openConnection();
if(timeout!=null) conn.setConnectTimeout(timeout);
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
// 建立實際的連接
conn.connect();
result.setHtml(HTTPParseFunc.inputStream2String(conn.getInputStream(), charset));
} catch (Exception e) {
e.printStackTrace();
result=null;
}
// 使用finally塊來關閉輸入流
finally {
try {
if (conn != null){
if(conn.getResponseCode()!=HttpURLConnection.HTTP_OK)
result=null;
result.setStatusCode(conn.getResponseCode());
}
} catch (Exception e) {
}
try {
if (in != null) {
in.close();
}
if (conn != null){
conn.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}

/**
* 向指定URL發送POST方法的請求
*
* @param url
* 發送請求的URL
* @param param
* 請求參數,請求參數應該是name1=value1&name2=value2的形式。
* @return URL所代表遠程資源的響應
*/
public static HTTPResponse sendPost(String url, String params, String charset,boolean isAutoRedirect, Integer timeout,Proxy proxy) {
HTTPResponse result=new HTTPResponse();
PrintWriter out = null;
BufferedReader in = null;
HttpURLConnection conn=null;
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接

HttpURLConnection.setFollowRedirects(isAutoRedirect);
if(proxy!=null)
conn = (HttpURLConnection)realUrl.openConnection(proxy);
else
conn = (HttpURLConnection)realUrl.openConnection();
if(timeout!=null) conn.setConnectTimeout(timeout);
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發送請求參數
if(params!=null) out.print(params);
// flush輸出流的緩衝
out.flush();
result.setHtml(HTTPParseFunc.inputStream2String(conn.getInputStream(), charset));

} catch (Exception e) {
e.printStackTrace();
result=null;
}
// 使用finally塊來關閉輸出流、輸入流
finally {
try {
if (conn != null){
if(conn.getResponseCode()!=HttpURLConnection.HTTP_OK)
result=null;
result.setStatusCode(conn.getResponseCode());
}
} catch (Exception e) {
}
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
if (conn != null){
conn.disconnect();
}
}
return result;
}


}

使用範例程式

package common.test;

import java.util.HashMap;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import common.HTTPBaseIO2;
import common.model.HTTPResponse;

public class HTTPBaseIO2Test {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testDoGet() {
String urltest = "http://localhost:8080/Test/Test1" ;
String charset="UTF-8";
HashMap<String, String> params=new HashMap<String, String>();
try {
params.put("val", "測試&測試");
HTTPResponse html=HTTPBaseIO2.doSend(urltest, HTTPBaseIO2.Method.get, params, charset,true,null,null);
System.out.println("testDoGet:"+html.getHtml());
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testDoPost() {
String urltest = "http://localhost:8080/Test/Test1" ;
String charset="UTF-8";
HashMap<String, String> params=new HashMap<String, String>();
try {
params.put("val", "測試1&測試1");
HTTPResponse html=HTTPBaseIO2.doSend(urltest, HTTPBaseIO2.Method.post, params, charset,true,null,null);
System.out.println("testDoPost:"+html.getHtml());
params.clear();
params.put("val", "測試2&測試2");
html=HTTPBaseIO2.doSend(urltest, HTTPBaseIO2.Method.post, params, charset,true,null,null);
System.out.println("testDoPost:"+html.getHtml());
} catch (Exception e) {
e.printStackTrace();
}
}

}
這裡是關於技術的手札~

也歡迎大家到

倫與貓的足跡



到噗浪來

關心一下我唷!
by 倫
 
Copyright 2009 倫倫3號Beta-Log All rights reserved.
Blogger Templates created by Deluxe Templates
Wordpress Theme by EZwpthemes