【问题标题】:How to access router IP using android?如何使用android访问路由器IP?
【发布时间】:2015-06-17 19:36:12
【问题描述】:

如何用android访问路由器ip?

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.195.1:80");
Log.d("test",post.toString());


try {
    HttpResponse response = client.execute(post);
    Log.d("test",response.toString());
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(response.getEntity().getContent()));
    String line="";
    while((line=reader.readLine())!=null){
        Log.d("test", line);
    }
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

我正在尝试使用此代码,但我在日志中收到连接被拒绝错误消息。我认为这是由于路由器身份验证。如何访问路由器?

【问题讨论】:

  • 你在做什么?因为 192.168.195.1 是你的网关?你为什么要发帖?无论如何,您收到此消息是因为您的路由器可能使用带有会话 id 的会话 cookie,并且大多数路由器拒绝端口 80 上的 http post...您应该尝试使用 get。
  • 我正在尝试访问路由器。你的意思是我应该使用 HttpGet 而不是 HttpPost?我尝试使用端口 80,因为当我尝试使用网络浏览器访问时,路由器正在使用端口 80。
  • 是的,但是在尝试使用 GET 发布任何命令之前,您必须打开一个会话...例如我在华为路由器上工作,首先我打开路由器页面以获取 cookie(使用 loopj作为库,因为它支持 cookie)然后我解析会话 ID(在 http 响应中)以执行下一个 HTTP CALL。主要问题是路由器不同,你必须检查你的路由器是如何工作的。我可以给你看我的代码,但是不同的路由器需要不同的代码
  • 感谢您的回复。如果您向我展示您的代码,将会很有帮助。谢谢。
  • 您可能正在寻找默认网关stackoverflow.com/a/5391763/2413303

标签: java android httpclient router


【解决方案1】:

我使用 loopj 创建一个 HTTPGet 来打开一个会话并保存 cookie,解析响应以获取 sessionId(在我的情况下,它需要向路由器发送 SOAP 命令)。将 AsyncHttpClient 的 istance 传递给第二种方法,会话仍然打开(但您可以将 AsyncHttpClient 定义为 Class 的对象,我不知道您的代码是如何编写的)并执行命令

 public void doCommandToRouter(final String channel, final genericResponse result) {
        final AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://" + Constants.WIFICHANNEL.VS_DEFAULT_IP, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] response) {
                String session_id = parseSessionId(new String(response));
                applyChanneltoStation(client,session_id,channel,result);
            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] response, Throwable throwable) {
                Log.d(TAG, "Fail");
                result.onError(i);
            }
        });
}
private void applyCommandtoRouter(final AsyncHttpClient client, final String session_id, final String bestChannel, final genericResponse result) {

    numTestChange = 0;
    String  xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">... SOAP COMMAND ...</soapenv:Envelope>";

    HttpEntity entity;
    try {
        entity = new StringEntity(xml, "UTF-8");
    } catch (IllegalArgumentException e) {
        Log.d("HTTP", "StringEntity: IllegalArgumentException");
        return;
    } catch (UnsupportedEncodingException e) {
        Log.d("HTTP", "StringEntity: UnsupportedEncodingException");
        return;
    }
    String  contentType = "text/xml";

    client.post( context, Constants.WIFICHANNEL.VS_SERVICE_URL, entity, contentType, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int i, Header[] headers, byte[] bytes) {
            Log.d(TAG, "post ok");
            //Log.d(TAG, "Response: " + new String(bytes));
            result.onSuccess();
        }

        @Override
        public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
            Log.d(TAG, "Fail. Error " + i);
        }

    });
}

解析响应

    private String parseSessionId(String pageText) {
    String pattern = "(dm_cookie=')(.+)(';)";
    Pattern p  = Pattern.compile(pattern);
    Matcher m = p.matcher(pageText);
    String session_id = "";
    while(m.find()) {
        session_id = m.group(2);
        Log.d(TAG, "Found session id: " + session_id);
    }
    return session_id;
}

请记住,这只适用于我的路由器,你必须检查你的路由器是如何工作的,因为 ctrl+c crtrl+v 可能不起作用。

【讨论】:

  • 我使用了 loopj,但它只执行 onFailure(),但在你的情况下 onSuccess() 正在执行。我检查状态代码,我收到 401 错误限制访问。我能做些什么呢?
  • 什么方法返回401?我使用的路由器不需要用户名和密码,路由器使用什么类型的autentication?
  • 我正在使用 TP Link 路由器,它正在使用基本访问身份验证,显示用户名和密码弹出窗口。
  • BulletProof47 您是否从 2013 年开始获得解决方案?我也有同样的困境
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 2016-04-07
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
相关资源
最近更新 更多