Java 在安卓API中,如果我限制了wifi,如何拦截http请求?
我正在尝试构建一个应用程序,将我的android wifi设置为对公众开放。当有人接近它时,它会为他们(或打开)一个网页,询问用户名和密码(以及其他一些东西)。身份验证需要通过运行在wifi tetherer(我的)手机上的数据库进行验证。如果认证通过,则用户通过我的手机连接到wifi。最好是插入html代码或重定向用户在连接到my wifi时发出的http请求Java 在安卓API中,如果我限制了wifi,如何拦截http请求?,java,android,http,wifi,android-wifi,Java,Android,Http,Wifi,Android Wifi,我正在尝试构建一个应用程序,将我的android wifi设置为对公众开放。当有人接近它时,它会为他们(或打开)一个网页,询问用户名和密码(以及其他一些东西)。身份验证需要通过运行在wifi tetherer(我的)手机上的数据库进行验证。如果认证通过,则用户通过我的手机连接到wifi。最好是插入html代码或重定向用户在连接到my wifi时发出的http请求 我该怎么建造这样的东西呢?我必须实现哪些API?为了让您的用户至少访问一次登录页面,您应该允许他们使用一些服务器技术进入Wifi。 您
我该怎么建造这样的东西呢?我必须实现哪些API?为了让您的用户至少访问一次登录页面,您应该允许他们使用一些服务器技术进入Wifi。 您可以在android中使用NanoHTTPD 这是参考资料 这就是移动服务器服务的外观
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;
}
}
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));
}
}
公共类Web服务{
私有静态最终字符串APPLICATION_JSON=“APPLICATION/JSON”;
私人NanoHTTPD.IHTTPSession会议;
公共Web服务(NanoHTTPD.IHTTPSession会话){
this.session=会话;
}
公共NanoHTTPD.Response get(){
Map response=newhashmap();
字符串值=this.session.getUri();
回应。投入(“价值”,价值);
response.put(“请求_参数”,GsonProvider.getInstance().getGson().toJson(this.session.getParms());
返回NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.ACCEPTED,APPLICATION_JSON,GsonProvider.getInstance().getGson().toJson(Response));
}
}
有关完整的运行示例,请在链接中克隆git存储库引用
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));
}
}