========================================
Thread xThread = new Thread(
delegate () {
HttpListener httpListener = new HttpListener();
httpListener.Prefixes.Add("http://localhost:2048/DoLocalJob/");
try {
httpListener.Start();
} catch (HttpListenerException hlex) {
Console.WriteLine(hlex);
return;
}
Console.WriteLine("Now ready to receive traces...");
while (true) {
var context = httpListener.GetContext(); // get te context
var data_text = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding).ReadToEnd();
Console.WriteLine("data_text is " + data_text);
var cleaned_data = System.Net.WebUtility.UrlDecode(data_text);
Console.WriteLine("clear data is " + cleaned_data);
Console.WriteLine("raw is " + context.Request.RawUrl);
byte[] buffer = Encoding.UTF8.GetBytes("OK");
HttpListenerResponse response = context.Response;
response.StatusCode = 200;
response.SendChunked = true;
//如果沒有以下幾個header設定的話,Ext.Ajax.Request所收到的response都是空白的!!
response.ContentType = "application/json; charset=utf-8";
response.AppendHeader("Access-Control-Allow-Origin", "*");
response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
response.AppendHeader("Access-Control-Allow-Headers", "Content-Type,x-prototype-version,x-requested-with");
response.ContentLength64 = buffer.Length;
Stream output = response.OutputStream;
if (context.Request.RawUrl.Contains("axn=MyAction")) {
doLocalJobProcess();
response.ContentLength64 = buffer.Length;
output.Write(buffer, 0, buffer.Length);
output.Flush();
continue;
}
buffer= Encoding.ASCII.GetBytes("parameter error!!");
output.Write(buffer, 0, buffer.Length);
Console.WriteLine("send back " + cleaned_data);
output.Flush();
output.Close();
}
});
xThread.Start();
local http server起來了,之後就是網頁的事了
=======================================
Ext.Ajax.request({
defaultHeaders: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
useDefaultXhrHeader: false,
cors: true,
url: 'http://localhost:2048/DoLocalJob/axn=MyAction',
method: 'POST',
params: {
xxx:yyyy
},
timeout: 10240,
success: function (response) {
if (response.responseText == 'OK') {
......
....
return;
}
//....其他錯誤....
return;
},
failure: function (response) {
//....http listener 沒起來,或是剛才說的header沒設好也會跑來這段
return;
}
});
===============================================