備忘錄_20160105(定位)
修改
回首頁
程式 2026-10-30 00:50:49 1793292649 100
aspx 4.x (using ashx)
test.ashx
<%@ WebHandler Language="C#" Class="API20261030090400" %>
// 注意,檔案編碼必須為 UTF-8-BOM
using System;
using System.Collections.Generic; // Dictionary
using System.IO;
using System.Web;
using System.Web.Script.Serialization; // JavaScriptSerializer
public class API20261030090400 : IHttpHandler
{
public bool IsReusable { get { return false; } }
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType="application/json; charset=utf-8";
JavaScriptSerializer oJSS=new JavaScriptSerializer();
Dictionary<string, object> oOutput=new Dictionary<string, object>();
oOutput["booSuccess"]=true;
oOutput["strErrorMessage"]="";
try
{
string strInput="";
using (StreamReader oSR=new StreamReader(context.Request.InputStream)) { strInput=oSR.ReadToEnd(); }
Dictionary<string, object> oInput=oJSS.Deserialize<Dictionary<string, object>>(strInput);
oOutput["oInput"]=oInput;
string[] straFullPath=Directory.GetFiles(context.Server.MapPath("~/text"));
Array.Sort(straFullPath);
Array.Reverse(straFullPath);
List<string> lstFile=new List<string>();
List<string> lstContent=new List<string>();
for(int i=0; i<straFullPath.Length; i++)
{
string strFullPath=straFullPath[i];
lstFile.Add(Path.GetFileName(strFullPath));
lstContent.Add(File.ReadAllText(strFullPath));
}
string[] straFile=lstFile.ToArray();
string[] straContent=lstContent.ToArray();
oOutput["straFile"]=straFile;
oOutput["straContent"]=straContent;
}
catch (Exception oE)
{
context.Response.StatusCode=500;
oOutput["booSuccess"]=false;
oOutput["strErrorMessage"]=HttpUtility.JavaScriptStringEncode(oE.Message);
}
finally
{
string strOutput=oJSS.Serialize(oOutput);
context.Response.Write(strOutput);
}
}
}
// 若要偵錯時,記得在網站根目錄的 web.config 加入 system.web 區塊
//
// <?xml version="1.0" encoding="UTF-8"?>
// <configuration>
//
// <system.web>
// <customErrors mode="Off" />
// <compilation debug="true" />
// </system.web>
//
// <system.webServer>
// <httpErrors>
// <remove statusCode="404" subStatusCode="-1" />
// <error statusCode="404" prefixLanguageFilePath="" path="/404.htm" responseMode="ExecuteURL" />
// </httpErrors>
// </system.webServer>
//
// </configuration>
//
test.htm
<!doctype html>
<html lang="zh-Hant-TW">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>your title</title>
</head>
<body>
<script>
window.addEventListener(
"load",
async function()
{
while(true)
{
try
{
var oResponse=await fetch(
"test.ashx",
{
method: "POST",
headers: { "Content-Type": "application/json; charset=UTF-8" },
body: JSON.stringify({"strUsername":"john", "strPassword":"123"})
}
);
var strJson=await oResponse.text();
var oJson=JSON.parse(strJson);
console.log(oJson);
}
catch(oError)
{
console.log({ "oResponse":oResponse, "strJson":strJson, "oError":oError });
alert((oResponse.url+"\n"+oResponse.status+"\n"+oResponse.statusText)+"\n\n"+strJson+"\r\n"+(oError.toString()));
break;
}
break;
}
}
);
</script>
</body>
</html>