在 android API 中,如果我断开了 wifi,我该如何拦截 http 请求?
In android APIs how do I go about intercepting a http request if I tether out my wifi?
我正在尝试构建一个应用程序,将我的 android wifi 设置为对 public 开放。当有人连接到它时,它会为他们提供(或打开)一个网页,要求输入用户名和密码(以及其他一些东西)。需要使用 wifi tetherer(我的)phone 上的数据库 运行 验证身份验证。如果身份验证通过,则用户通过我的 phone 连接到 wifi。最好的方法是注入 html 代码或重定向用户在连接到我的 wifi 时发出的 http 请求。
我该如何构建这样的东西?我必须实施哪些 API?
为了让您的用户至少访问登录页面一次,您应该允许他们使用某种服务器技术接入 Wifi。
您可以在 android.
中使用 NanoHTTPD
这里是参考link
这就是您的移动服务器服务的样子
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
/**
* Created by Ashish on 10/01/16.
*/
public class ServerService extends Service {
private static final int DEFAULT_PORT = 8912;
private Server server;
private BroadcastReceiver broadcastReceiverNetworkState;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String ip = getIpAccess();
server = new Server(DEFAULT_PORT);
try {
server.start();
showNotification(ip);
} catch (IOException e) {
e.printStackTrace();
}
return START_STICKY;
}
@Override
public void onDestroy() {
server.stop();
super.unregisterReceiver(broadcastReceiverNetworkState);
super.onDestroy();
}
private String getIpAccess() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
final String formatedIpAddress = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
return "http://" + formatedIpAddress + ":" + DEFAULT_PORT;
}
private void showNotification(String serverAddress) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Server Running")
.setAutoCancel(false)
.setCategory("SystemServer")
.setContentText(serverAddress);
int mNotificationId = 110;
startForeground(mNotificationId, mBuilder.build());
}
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
}
Server.java
import fi.iki.elonen.NanoHTTPD;
import in.ashish29agre.androidnanohttpd.services.WebService;
/**
* Created by Ashish on 10/01/16.
*/
public class Server extends NanoHTTPD {
private static final String APPLICATION_JSON = "application/json";
public Server(int port) {
super(port);
}
public Server(String hostname, int port) {
super(hostname, port);
}
@Override
public Response serve(IHTTPSession session) {
return new WebService(session).get();
}
}
WebService.java
public class WebService {
private static final String APPLICATION_JSON = "application/json";
private NanoHTTPD.IHTTPSession session;
public WebService(NanoHTTPD.IHTTPSession session) {
this.session = session;
}
public NanoHTTPD.Response get() {
Map<String, String> response = new HashMap<>();
String value = this.session.getUri();
response.put("value", value);
response.put("request_parameters", GsonProvider.getInstance().getGson().toJson(this.session.getParms()));
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.ACCEPTED, APPLICATION_JSON, GsonProvider.getInstance().getGson().toJson(response));
}
}
要获得完整的 运行 示例,请在 link.
中克隆 git 存储库引用
我正在尝试构建一个应用程序,将我的 android wifi 设置为对 public 开放。当有人连接到它时,它会为他们提供(或打开)一个网页,要求输入用户名和密码(以及其他一些东西)。需要使用 wifi tetherer(我的)phone 上的数据库 运行 验证身份验证。如果身份验证通过,则用户通过我的 phone 连接到 wifi。最好的方法是注入 html 代码或重定向用户在连接到我的 wifi 时发出的 http 请求。
我该如何构建这样的东西?我必须实施哪些 API?
为了让您的用户至少访问登录页面一次,您应该允许他们使用某种服务器技术接入 Wifi。 您可以在 android.
中使用 NanoHTTPD这里是参考link
这就是您的移动服务器服务的样子
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
/**
* Created by Ashish on 10/01/16.
*/
public class ServerService extends Service {
private static final int DEFAULT_PORT = 8912;
private Server server;
private BroadcastReceiver broadcastReceiverNetworkState;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String ip = getIpAccess();
server = new Server(DEFAULT_PORT);
try {
server.start();
showNotification(ip);
} catch (IOException e) {
e.printStackTrace();
}
return START_STICKY;
}
@Override
public void onDestroy() {
server.stop();
super.unregisterReceiver(broadcastReceiverNetworkState);
super.onDestroy();
}
private String getIpAccess() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
final String formatedIpAddress = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
return "http://" + formatedIpAddress + ":" + DEFAULT_PORT;
}
private void showNotification(String serverAddress) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Server Running")
.setAutoCancel(false)
.setCategory("SystemServer")
.setContentText(serverAddress);
int mNotificationId = 110;
startForeground(mNotificationId, mBuilder.build());
}
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
}
Server.java
import fi.iki.elonen.NanoHTTPD;
import in.ashish29agre.androidnanohttpd.services.WebService;
/**
* Created by Ashish on 10/01/16.
*/
public class Server extends NanoHTTPD {
private static final String APPLICATION_JSON = "application/json";
public Server(int port) {
super(port);
}
public Server(String hostname, int port) {
super(hostname, port);
}
@Override
public Response serve(IHTTPSession session) {
return new WebService(session).get();
}
}
WebService.java
public class WebService {
private static final String APPLICATION_JSON = "application/json";
private NanoHTTPD.IHTTPSession session;
public WebService(NanoHTTPD.IHTTPSession session) {
this.session = session;
}
public NanoHTTPD.Response get() {
Map<String, String> response = new HashMap<>();
String value = this.session.getUri();
response.put("value", value);
response.put("request_parameters", GsonProvider.getInstance().getGson().toJson(this.session.getParms()));
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.ACCEPTED, APPLICATION_JSON, GsonProvider.getInstance().getGson().toJson(response));
}
}
要获得完整的 运行 示例,请在 link.
中克隆 git 存储库引用