Show toolbar

2014年3月26日 星期三

C# POST using WebRequest

標題:C#使用WebRequest POST後取得回傳資料
C# (Program.cs):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net; //WebRequest WebResponse
using System.IO; //Stream
public class Global
{
public static string server = "http://127.0.0.1:1337";
}
namespace WebPostRequest
{
class Program
{
static string login(string email, string password)
{
//宣告送出資料
string param = "email=" + email + "&password=" + password;
byte[] byteArray = Encoding.UTF8.GetBytes(param);
//Request資料
WebRequest request = WebRequest.Create(Global.server + "/login");
request.Credentials = CredentialCache.DefaultCredentials;
((HttpWebRequest)request).UserAgent = ".NET Framework Example Client";
request.Method = "POST";
request.ContentLength = byteArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
//處理資料流
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//Response資料
WebResponse response = request.GetResponse();
string rawJsonString = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
return rawJsonString;
}
static void Main(string[] args)
{
Console.WriteLine( login("test@gmail.com", "abcd1234") );
Console.WriteLine("\r\nPress enter key to continue....");
Console.Read();
}
}
}

說明:
簡單使用WebRequest POST送出email、password後回傳字串資料的作法。

2014年3月11日 星期二

Dynamic get jsonp

標題:動態取得Jsonp資料
JavaScript (index.html):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<div id="output_getjsonp"></div>
<script type="text/javascript">
var getjsonp = function() {
this.head = document.getElementsByTagName('head')[0] || document.documentElement;
this.current = null;
this.queue = [];
};
getjsonp.prototype.request = function(q) {
var fid = (q.fid||'_getjsonp') + new Date().getTime();
console.log(fid);
var script = document.createElement('script');
script.src = q.url.replace('callback=?', 'callback=' + fid);
this.queue.push({
fid : fid,
script : script,
load : q.load
});
if(!this.current) this.action();
};
getjsonp.prototype.action = function() {
var self = this;
this.current = null;
if(this.queue.length) {
this.current = this.queue.shift();
window[this.current.fid] = function(data) {
self.head.removeChild(self.current.script);
self.current.load && self.current.load(data);
self.action();
};
this.head.appendChild(this.current.script);
}
};
var jsonp = new getjsonp();
jsonp.request({
fid : '_myjsonp',
url : 'http://codeboxy.blogspot.com/feeds/posts/default?alt=json&callback=?',
load : function(data) {
document.getElementById('output_getjsonp').innerHTML = data.feed.entry[0].title.$t;
}
});
</script>

範例結果:
Mask String

說明:
動態使用jsonp取得json資料,測試支援版本:
  • Google Chrome 33.0.1750.146 m
  • Firefox 27.0.1
  • IE 11.0.9600.16518

2014年3月10日 星期一

JavaScript Advanced Logical Operators

標題:JavaScript邏輯運算子進階用法
JavaScript (main.js):
1
2
3
4
5
6
7
8
9
10
11
12
function foo() {
console.log('Hello');
return true;
}
function bar() {
console.log('World');
return false;
}
foo() && bar(); //Hello World
bar() && foo(); //World
foo() || bar(); //Hello
bar() || foo(); //World Hello

說明:
1. foo()為true 則會繼續執行bar()
2. bar()為false不會繼續執行foo()
3. foo()為true 不會繼續執行bar()
4. bar()為false則會繼續執行foo()

2014年3月6日 星期四

Fluent Interface in exports of Node.js

標題:Node.js的exports使用流暢介面寫法
Node.js (main.js):
1
2
3
4
var my = require('./count.js');
var foo = new my.foo(10, 2);
foo.plus().say();
foo.times().say();
exports js (count.js):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var foo = function(m, n) {
this.m = m;
this.n = n;
this.answer = 0;
this.method = "";
};
foo.prototype.plus = function() {
this.answer = this.m + this.n;
this.method = "+";
return this;
};
foo.prototype.times = function() {
this.answer = this.m * this.n;
this.method = "*";
return this;
};
foo.prototype.say = function() {
console.log("%d %s %d = %d", this.m, this.method, this.n, this.answer);
return this;
};
exports.foo = foo;
範例結果:


說明:
在Node.js中採用流暢介面寫法,並使用exports建立一個外部js,在主程式中require後使用。