サンプル10

ひかり電話CTIスマートコネクトの機能を利用して、CTIサーバーにあるファイルをローカルにコピーするc#サンプルです。

<事前に準備が必要>

CTIサーバーに「ひかり電話CTIスマートコネクト」をインストールしてください。

※「ひかり電話CTIスマートコネクト」は、ひかり電話CTIクライアントのインストールパッケージの中に同梱されています。

 

<Visual Studio 内でやること>

・参照の追加で、アセンブリ System.ServiceModel を追加する。

・サービス参照の追加で

http://(CTIサーバーのアドレス):54215/nsFileManagerServiceLibrary.FileManagerServiceLibrary/mex

を追加する。

 

<サンプルプログラム c#>

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.IO;

namespace HikariCTI_Sample_010
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                EndpointAddress endPoint = new EndpointAddress(
                    "http://192.168.24.9:54215/nsFileManagerServiceLibrary.FileManagerServiceLibrary");

                BasicHttpBinding httpBind = new BasicHttpBinding();
                httpBind.MaxReceivedMessageSize = 2147483647;
                httpBind.SendTimeout = TimeSpan.Parse("00:10:00");
                httpBind.TransferMode = TransferMode.Streamed;

                ServiceReference1.IFileManagerServiceLibraryChannel proxy
                    = ChannelFactory.CreateChannel(httpBind, endPoint);

                /////////////////////////////////////////////////
                // CTIサーバーのファイルをローカルにコピーする //
                /////////////////////////////////////////////////

                //1.CTIサーバー側のファイルを指定
                Stream stm = proxy.GetStream(@"C:\Users\marusato\Documents\Hikari Denwa CTI\db_backup\hd_cti.sdf");
                
                //2.ローカル側にダウンロード
                DownloadFileTo(stm, @"C:\Users\智史\Desktop\hd_cti.sdf");


                stm.Close();
                stm.Dispose();

                proxy.Close();

                Console.WriteLine("ファイルをコピーしました。");

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                //メッセージを受信できる http://127.0.0.1:54215/nsFileManagerServiceLibrary.FileManagerServiceLibrary でリッスンしているエンドポイントがありませんでした。
                //これは一般に、アドレスまたは SOAP アクションが正しくない場合に発生します。詳細については、InnerException を参照してください (ある場合)。
                if (ex.Message.IndexOf("メッセージを受信できる") >= 0 &&
                    ex.Message.IndexOf("でリッスンしているエンドポイントがありませんでした") >= 0 &&
                    ex.Message.IndexOf("アドレスまたは SOAP アクションが正しくない場合に発生します") >= 0)
                {
                    Console.WriteLine("ひかり電話CTIスマートコネクトがインストールされていないか、\r\nサービスが開始されていないません。");

                    Console.ReadLine();
                }
            }
        }

        public static void DownloadFileTo(Stream stm, string fileName)
        {
            using (var file = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
            {
                int read;
                byte[] buffer = new byte[1024];
                while ((read = stm.Read(buffer, 0, buffer.Length)) > 0)
                {
                    file.Write(buffer, 0, read);
                }
            }
        }
    }
}