2009年11月25日 星期三

Sqlite with C# 在資料庫新增字串

這次試者將 RSS 下載下來的資料透過 System.Data.SQLite.dll 儲存到SQLite中 , 但是 會遇到一個問題就是 文章中會包涵 單引號(') 在用 sql的 insert 指令就會有問題


目前只想到一個方法就是吧字串存成 BLOB 的方式來避免錯誤

舉例說明比較容易

假設我要輸入一組 文字 如 文章標題,內容

標題: Tuck's incoming class gives back to the Upper Valley
內容: Before the academic year officially started, the incoming class of 2011 at the Tuck School of Business at Dartmouth was busy working on consulting projects for organizations in the Upper Valley. As part of Community Outreach Day (COD), all first-year students donated half a day during orientation week to work on solving a variety of issues facing local nonprofits.

將 標題的文字存成 一個 string 叫 title
內容 的文字存成 另一個 string 叫 contant

在將string 轉成 byte array 便可以 blob 方式 存入 資料庫

/// Translation string to byte array
byte[] bTitle = Encoding.ASCII.GetBytes(title);
byte[] bContant = Encoding.ASCII.GetBytes(contant);

轉貼 C# 文字轉成日期方法

有時候利用網路上取得的資料,但時間格式常常不是想要的格式,在這個時候就可以利用這個方法將說明時間的字串轉換為date 的格式

這時可以用 DateTime.ParseExact 的方法


主要有三個變數
s
型別:System.String
字串,包含要轉換的日期和時間。
format
型別:System.String
格式規範,定義 s 的所需格式。
provider
型別:System.IFormatProvider
物件,提供關於 s 的文化特性特定格式資訊。

如想要吧 這個字串
Tue, 10 Nov 2009 13:54:20 EST

轉成 DateTime 格式
以下是範例code




/// 宣告 格式資訊
CultureInfo provider = CultureInfo.InvariantCulture;
/// 時間字串
string pubDate="Tue, 10 Nov 2009 13:54:20 EST";
/// 轉換成DateTime 格式
DateTime x = DateTime.ParseExact(pubDate, "ddd, dd MMM yyyy HH':'mm':'ss 'EST'", provider);




詳細說明可以參考 MSDN

2009年11月12日 星期四

使用 Google protrol to get the google api in C# with Google Site data api

在這個範例 中 使用 Google protocol 去取得 google site data 的資訊

所使用的語言是 C#, java的版本之前在其他文章有寫過


Http Post requset with java:
使用 java http post 取得 google account (AuthSub)

Http Get request with java:
使用 java http Get( by AuthSub) 取得 google document list





主要分成3個部份

1. SetProxyServer (有經過proxy 才要設 )
code 如下

/**
* @name SetProxyServer
*
* Set the Proxy Server if have Proxy Server
*
* @param req [in] - the http web request
*/
public void SetProxyServer(HttpWebRequest req)
{

IWebProxy iProxy = WebRequest.DefaultWebProxy;

/// potentially, setup credentials on the proxy here
iProxy.Credentials = CredentialCache.DefaultCredentials;
req.Proxy = iProxy;
}






2.
取得 google api 的要驗證碼( 使用 http post), 這裡我是使用client login的方法

輸入以下三種資訊
Account: google 帳號
Password: google 帳號的密碼
Service Type: google api 的 類型 這裡使用 google site 類型為 "jotspot" (其他類型 )

code 如下



/**
* @name GetGoogleLoginAuth
*
* Get the google client login auth token
*
* @param Account [in] - the google account
* @param Password [in] - the google account password
* @param ServiceType [in] - the google api type
*
* @return the google client login auth
*/
public string GetGoogleLoginAuth(string Account, string Password, string ServiceType)
{
/// Declare the return valure
string GoogleLoginAuth="";

/// Create the request obj
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin");

/// Set proxy
this.SetProxyServer(req);

/// Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string strNewValue = "accountType=HOSTED_OR_GOOGLE"+
"&Email=" + Account +
"&Passwd=" + Password +
"&service=" + ServiceType +
"&source=Gulp-CalGulp-1.05";

/// Set the request value
req.ContentLength = strNewValue.Length;


/// Write the request
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(strNewValue);
stOut.Close();


/// Do the request to get the response
StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = stIn.ReadToEnd();


/// Split string and set token
/// there are tree type in the return value
/// SID, LSID and Auth
string[] parts = strResponse.Split('=');

/// Save the auth token, auth token is in last part of string parts[]
GoogleLoginAuth = parts[parts.Length - 1];

/// Close the stream reader
stIn.Close();


return GoogleLoginAuth;
}




3. RetrievalContentFeed , 利用 http get 取得google site 的資訊
主要實做 google guide 的範例

輸入兩個 資訊
a. url: http(s)://sites.google.com/feeds/content/site/siteName

site 網域名稱 如 example.com
siteName 網頁名稱 如 myCoolSite

b. token 剛取得的 auth token


附註:
要 傳送出以下的訊息
GET /feeds/content/site/siteName HTTP/1.1
Host: sites.google.com
GData-Version: 1.0
Authorization: GoogleLogin auth= yourAuthToken

在c # 這裡是用 HttpWebRequest.Headers.Add 來附加訊息
此外 Host 不需在設定, 因為 會從 url 取得
所以要附加的資訊就只有
GData-Version 和 Authorization

看下面的code 會更瞭解, code 如下


/**
* @name RetrievalContentFeed
*
* Get the google client login auth token
*
* @param URL [in] - the google api Url
* @param Token [in] - the google client login token
*
*
* @return Content Feed
*/
public string RetrievalContentFeed(string URL, string Token)
{
/// Declare the return valure
string Feed = "";

/// Create the request obj
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://sites.google.com/feeds/content/hh-mud.com/touch-teach");

/// Set proxy
this.SetProxyServer(req);

/// Set values for the request back
req.Method = "GET";

/// Set the Head value
req.Headers.Add("GData-Version:1.0");
req.Headers.Add("Authorization: GoogleLogin auth=" + Token);


/// Do the request to get the response
StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
Feed = stIn.ReadToEnd();

stIn.Close();

return Feed;
}


2009年11月11日 星期三

轉貼java sock 範例

透過 socket 可以讓兩台電腦達成連線並且互相傳訊訊息

在範例中 利用java建立
有兩個部份
server 端
client 端

client 透過連線可以和server 端溝通


直接轉貼範例

永遠的UNIX- Java做一個最簡單的Socket通話程序