从Android应用程序向WiFi Direct客户端显示静态html页面。 (就像WiFi Captive Portal)

时间:2018-11-28 06:21:14

标签: android sockets ssl https httpresponse

我们开发了一个WiFi Direct应用程序,我们希望在其中每次尝试访问Internet时向WiFi Direct Group Client显示一个静态html页面。我们正在使用套接字捕获其HTTP / HTTPS请求并作为响应返回静态页面。

该逻辑与不使用https的网站完美配合。例如,如果我打开www.fast.com,则静态html页面会返回给客户端,但是如果我打开www.youtube.com,则Google Chrome浏览器会引发以下错误:

  

ERR_TUNNEL_CONNECTION_FAILED

tunnel_error

以下是我们的代码:

public void returnLocalHtml(ParseRequest request) {
    BufferedReader reader = null;
    PrintWriter writer = null;
    InputStream inputStream = null;
    try {
        writer = new PrintWriter(socket.getOutputStream(), true);
        if (request.flag && request.requestType.equals("CONNECT")) {
            writer.print("HTTP/1.1 403" + "\r\n");
            writer.print("Content type: text/html" + "\r\n");
            writer.print("Content length: " + 0  + "\r\n");
            writer.flush();
        }
        inputStream = context.getAssets().open("index.html");
        reader = new BufferedReader(new InputStreamReader(inputStream));
        String response = "";
        byte[] value = new byte[4096];
        do {
            int read = inputStream.read(value);
            if (read == -1) {
                break;
            }
            response = response + new String(value, 0, read);
        } while (inputStream.available() > 0);
        writer.flush();
        writer.print("HTTP/1.1 200\r\n");
        writer.print("Content type: text/html" + "\r\n");
        writer.print("Content length: " + response.length() + "\r\n");
        writer.print("\r\n");
        writer.print(response + "\r\n");
        writer.flush();

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            writer.close();
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

我们认为我们做错了创建HTTP响应

任何帮助将不胜感激。

谢谢!!

0 个答案:

没有答案