autojs实现http

应用介绍

importClass(java.io.BufferedReader);
importClass(java.io.BufferedWriter);
importClass(java.io.IOException);
importClass(java.io.InputStreamReader);
importClass(java.io.OutputStreamWriter);
importClass(java.net.ServerSocket);
importClass(java.net.Socket);
importClass(java.nio.ByteBuffer);
importClass(java.io.PrintWriter);
 
// 字符串转bytes
function strToUtf8Bytes(str) {
    const utf8 = [];
    for (let ii = 0; ii < str xss=removed>> 6), 0x80 | (charCode & 0x3f));
        } else if (charCode < 0xd800>= 0xe000) {
            utf8.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
        } else {
            ii++;
            // Surrogate pair:
            // UTF-16 encodes 0x10000-0x10FFFF by subtracting 0x10000 and
            // splitting the 20 bits of 0x0-0xFFFFF into two halves
            charCode = 0x10000 + (((charCode & 0x3ff) << 10>> 18),
                0x80 | ((charCode >> 12) & 0x3f),
                0x80 | ((charCode >> 6) & 0x3f),
                0x80 | (charCode & 0x3f),
            );
        }
    }
    //兼容汉字,ASCII码表最大的值为127,大于127的值为特殊字符
    for (let jj = 0; jj < utf8 xss=removed> 127) {
            utf8[jj] = code - 256;
        }
    }
    return utf8;
}
 
var server;
// 退出时结束服务
events.on('exit', () => {
    if (server && server.close) {
        server.close();
    }
});
try {
    server = new ServerSocket(9000); // 9000为端口号
    while (true) {
        console.log("启动服务器....");
        var socket = server.accept();
        var is = socket.getInputStream();
        is.read(util.java.array('byte', 2048));
        var os = socket.getOutputStream();
        os.write(strToUtf8Bytes("HTTP/1.1 200 OK\r\n"));
        os.write(strToUtf8Bytes("Content-Type:text/html;charset=utf-8\r\n"));
        os.write(strToUtf8Bytes("Content-Length:38\r\n"));
        os.write(strToUtf8Bytes("Server:gybs\r\n"));
        os.write(strToUtf8Bytes(("Date:"+new Date()+"\r\n")));
        os.write(strToUtf8Bytes("\r\n"));
        os.write(strToUtf8Bytes("

hello!

"));         os.write(strToUtf8Bytes("

HTTP服务器!

"));         os.close();         socket.close();         toastLog("请求已响应");         // 此处放置接收到http请求时需要执行的代码     }     if (server && server.close) {         server.close();     } } catch (e) {     console.log("error:", e);     if (server && server.close) {         server.close();     } }

点赞(1) 打赏

立即下载

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部
0.200278s