2016-01-18 17 views
0

我試圖建立一個應用程序,將設置我的android wifi向公衆開放。當有人關閉連接到它時,它會爲他們提供(或打開)一個請求用戶名和密碼(以及其他一些東西)的網頁。驗證將需要通過無線網絡(我的)手機上運行的數據庫進行驗證。如果認證通過,則用戶通過手機連接到WiFi。最好的做法是注入html代碼或重定向用戶在連接到我的wifi時進行的http請求。在android API中,如果我阻止了我的wifi,我該如何去攔截一個http請求?

我該如何去建造這樣的東西?我必須實現哪些API?

回答

0

對於您的用戶至少要訪問登錄頁面一次,您應該允許他們使用某種服務器技術進入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)); 
    } 
} 

有關完整的運行示例,請在鏈接中克隆git存儲庫參考。

相關問題