android设置打印机wifi(OKHttp+firefox的插件:HttpRequester+WireShake + servlet)

本文主要是介绍android设置打印机wifi(OKHttp+firefox的插件:HttpRequester+WireShake + servlet),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

android设置打印机wifi(OKHttp+firefox的插件:HttpRequester+WireShake + servlet)。post和put提交。

通过android手机设置打印机的wifi步骤(爬虫):

  • 1.通过chrome捕获按钮所提交的请求数据
  • 2.通过WireShark捕获所提交的请求数据
  • 3.通过HttpRequester模拟浏览器提交请求数据
  • 4.引用OkHttp实现模拟浏览器提交请求数据
  • 5.(可不看)通过servlet查看post和put请求的数据

步骤1、2(先post后put):

下面图的都是‘点击应用提交的是post请求’的截图(即打开打印机的wifi开关):

这里写图片描述

这里写图片描述

这里写图片描述

由于上图的chrome找不到post的表单数据,所以借助WireShark抓包
这里写图片描述
这里写图片描述

下面的图是通过put方式提交wifi的账号和密码给服务器(这里的服务器指的是打印机)截图:

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

步骤3(HttpRequester插件模拟浏览器提交post和put请求)

HttpRequester插件提交post请求来打开打印机的wifi开关

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

HttpRequester插件提交put请求来提交wifi账号和密码

这里写图片描述

这里写图片描述

步骤4

1.导包

compile 'com.squareup.okhttp3:okhttp:3.9.0'

2.java实现


import java.io.IOException;import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;/*** Created by shixing on 2017/11/1.*/public class NetworkSetting {private static final OkHttpClient mOkHttpClient = new OkHttpClient();private static final String URL_POST = "http://192.168.223.1/hp/device/config_result_YesNo.html/config";private static final String URL_PUT = "http://192.168.223.1/IoMgmt/Adapters/wifi0/Profiles/Active";static final MediaType XML = MediaType.parse("application/xml");//打开wifi开关public static String postData() {FormBody.Builder formBodyBuilder = new FormBody.Builder();formBodyBuilder.add("Clear", "\346\230\257");formBodyBuilder.add("Menu", "NetWifiChange");formBodyBuilder.add("Configuration", "WifiEnabled=1");RequestBody requestBody = formBodyBuilder.build();Request request = new Request.Builder().addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8").addHeader("Accept-Encoding", "gzip, deflate").addHeader("Accept-Language", "zh-CN,zh;q=0.8").addHeader("Cache-Control", "max-age=0").addHeader("Connection", "keep-alive").addHeader("Content-Type", "application/x-www-form-urlencoded").addHeader("Host", "192.168.223.1").addHeader("Origin", "http://192.168.223.1").addHeader("Referer", "http://192.168.223.1/hp/device/config_result_YesNo.html?tab=Networking&amp;menu=NetWifiChange&amp;WifiEnabled=1").addHeader("Upgrade-Insecure-Requests", "1").addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36").url(URL_POST).post(requestBody).build();try {Response response = mOkHttpClient.newCall(request).execute();Log.d(MainActivity.TAG, "postData: " + response.code());return response.body().string();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}//把用户名和密码以一个xml字符串的形式put到打印机(这里的打印机就是服务器)上public static String putMyData() {RequestBody requestBody = RequestBody.create(XML,getXml());Request request = new Request.Builder().url(URL_PUT).addHeader("Accept","application/xml, text/xml, */*").addHeader("Accept-Encoding","gzip, deflate").addHeader("Connection","keep-alive").addHeader("Accept-Language","zh-CN,zh;q=0.8").addHeader("Host","192.168.223.1").addHeader("Origin","http://192.168.223.1").addHeader("Referer","http://192.168.223.1/set_config_network_Wireless.html?tab=Networking&menu=NetWireless").addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36").addHeader("X-Requested-With","XMLHttpRequest").put(requestBody).build();try {Response response = mOkHttpClient.newCall(request).execute();Log.d(MainActivity.TAG, "putMyData: " + response.code());return response.body().string();} catch (IOException e) {e.printStackTrace();}return null;}private static String getXml() {return "<?xml version:\"1.0\" encoding=\"UTF-8\" ?><io:Profile  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dd=\"http://www.hp.com/schemas/imaging/con/dictionaries/1.0/\" xmlns:io=\"http://www.hp.com/schemas/imaging/con/ledm/iomgmt/2008/11/30\" xmlns:wifi=\"http://www.hp.com/schemas/imaging/con/wifi/2009/06/26\" xmlns:ipdyn=\"http://www.hp.com/schemas/imaging/con/ledm/internalprintdyn/2008/03/21\" ><io:AdapterProfile><io:WifiProfile><wifi:SSID>646C696E6B</wifi:SSID> <wifi:CommunicationMode>infrastructure</wifi:CommunicationMode> <wifi:EncryptionType>WPA_PSK</wifi:EncryptionType><wifi:AuthenticationMode>WPA_PSK</wifi:AuthenticationMode><io:KeyInfo><io:WpaPassPhraseInfo><wifi:RsnEncryption>AESOrTKIP</wifi:RsnEncryption><wifi:RsnAuthorization>autoWPA</wifi:RsnAuthorization><wifi:PassPhrase>6E686C323230323230</wifi:PassPhrase></io:WpaPassPhraseInfo></io:KeyInfo></io:WifiProfile></io:AdapterProfile></io:Profile>";}
}

MainActivity.java(不要在ui线程访问网络)

public class MainActivity extends AppCompatActivity {public static final String TAG = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);new Thread() {@Overridepublic void run() {Log.d(TAG, "run: " + NetworkSetting.postData());Log.d(TAG, "run: " + NetworkSetting.putMyData());}}.start();}
}

步骤5 (通过servlet查看post和put请求的数据)

1.把client端的URL_POST改为:

private static String URL_POST = "http://localhost:8080/TestForm/FormServlet";

2.servlet代码


import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FormServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("get");doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("post");System.out.println("content-length:" + req.getContentLength());System.out.println("......................");Enumeration<String> attrs = req.getAttributeNames();String strAttr = "attributes:";while(attrs.hasMoreElements()) {strAttr = strAttr + attrs.nextElement() + ", ";}System.out.println(strAttr);System.out.println("......................");Enumeration<String> names = req.getHeaderNames();String strName = "attribute names:";while(names.hasMoreElements()) {String headName = names.nextElement();strName = strName + headName+ ":" + req.getHeader(headName) + ", ";}System.out.println(strName);System.out.println("......................");System.out.println("authType:" + req.getAuthType() + ", characterEncoding:" + req.getCharacterEncoding()+", contentType:" + req.getContentType() + ", queryString:" + req.getQueryString());System.out.println("......................");Enumeration<String> params = req.getParameterNames();String strParamNames = "params names:";while(params.hasMoreElements()) {strParamNames = strParamNames + params.nextElement() + ", ";}System.out.println("......................");System.out.println(strParamNames);System.out.println("......................");Map<String, String[]> paramsMaps = req.getParameterMap();for(String key : paramsMaps.keySet()) {String [] values = paramsMaps.get(key);String strVal = "";for(String str: values) {strVal = strVal + str + ", ";}System.out.println("key=" + key +", values=" + strVal);}BufferedReader br = req.getReader();System.out.println("======================");System.out.println("bufferReader:");String str = "";while((str = br.readLine()) != null) {System.out.println(str);}resp.sendRedirect(req.getContextPath()+"/haha.html");}@Overrideprotected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPut(req, resp);System.out.println("put");System.out.println("content-length:" + req.getContentLength());System.out.println("......................");Enumeration<String> attrs = req.getAttributeNames();String strAttr = "attributes:";while(attrs.hasMoreElements()) {strAttr = strAttr + attrs.nextElement() + ", ";}System.out.println(strAttr);System.out.println("......................");Enumeration<String> names = req.getHeaderNames();String strName = "attribute names:";while(names.hasMoreElements()) {String headName = names.nextElement();strName = strName + headName+ ":" + req.getHeader(headName) + ", ";}System.out.println(strName);System.out.println("......................");System.out.println("authType:" + req.getAuthType() + ", characterEncoding:" + req.getCharacterEncoding()+", contentType:" + req.getContentType() + ", queryString:" + req.getQueryString());System.out.println("......................");Enumeration<String> params = req.getParameterNames();String strParamNames = "params names:";while(params.hasMoreElements()) {strParamNames = strParamNames + params.nextElement() + ", ";}System.out.println("......................");System.out.println(strParamNames);System.out.println("......................");Map<String, String[]> paramsMaps = req.getParameterMap();for(String key : paramsMaps.keySet()) {String [] values = paramsMaps.get(key);String strVal = "";for(String str: values) {strVal = strVal + str + ", ";}System.out.println("key=" + key +", values=" + strVal);}BufferedReader br = req.getReader();System.out.println("======================");System.out.println("bufferReader:");String str = "";while((str = br.readLine()) != null) {System.out.println(str);}resp.sendRedirect(req.getContextPath()+"/haha.html");}
}

服务器收到的post请求数据如下:

post
content-length:73
......................
attributes:
......................
attribute names:accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8, accept-encoding:gzip, deflate, accept-language:zh-CN,zh;q=0.8, cache-control:max-age=0, connection:keep-alive, host:192.168.223.1, origin:http://192.168.223.1, referer:http://192.168.223.1/hp/device/config_result_YesNo.html?tab=Networking&amp;menu=NetWifiChange&amp;WifiEnabled=1, upgrade-insecure-requests:1, user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36, content-type:application/x-www-form-urlencoded, content-length:73, 
......................
authType:null, characterEncoding:null, contentType:application/x-www-form-urlencoded, queryString:null
......................
......................
params names:Clear, Menu, Configuration, 
......................
key=Clear, values=是, 
key=Menu, values=NetWifiChange, 
key=Configuration, values=WifiEnabled=1, 
======================
bufferReader:

服务器收到的put请求数据如下:

put
content-length:916
......................
attributes:
......................
attribute names:accept:application/xml, text/xml, */*, accept-encoding:gzip, deflate, connection:keep-alive, accept-language:zh-CN,zh;q=0.8, host:192.168.223.1, origin:http://192.168.223.1, referer:http://192.168.223.1/set_config_network_Wireless.html?tab=Networking&menu=NetWireless, user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36, x-requested-with:XMLHttpRequest, content-type:application/xml, content-length:916, 
......................
authType:null, characterEncoding:null, contentType:application/xml, queryString:null
......................
......................
params names:
......................
======================
bufferReader:
<?xml version:"1.0" encoding="UTF-8" ?><io:Profile  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dd="http://www.hp.com/schemas/imaging/con/dictionaries/1.0/" xmlns:io="http://www.hp.com/schemas/imaging/con/ledm/iomgmt/2008/11/30" xmlns:wifi="http://www.hp.com/schemas/imaging/con/wifi/2009/06/26" xmlns:ipdyn="http://www.hp.com/schemas/imaging/con/ledm/internalprintdyn/2008/03/21" ><io:AdapterProfile><io:WifiProfile><wifi:SSID>646C696E6B</wifi:SSID> <wifi:CommunicationMode>infrastructure</wifi:CommunicationMode> <wifi:EncryptionType>WPA_PSK</wifi:EncryptionType><wifi:AuthenticationMode>WPA_PSK</wifi:AuthenticationMode><io:KeyInfo><io:WpaPassPhraseInfo><wifi:RsnEncryption>AESOrTKIP</wifi:RsnEncryption><wifi:RsnAuthorization>autoWPA</wifi:RsnAuthorization><wifi:PassPhrase>6E686C323230323230</wifi:PassPhrase></io:WpaPassPhraseInfo></io:KeyInfo></io:WifiProfile></io:AdapterProfile></io:Profile>

结论:

从服务器收到的post和put的请求数据对比得出,put的xml字符串通过req.getReader()来读取,而post提交的表单则通过
Map<String, String[]> paramsMaps = req.getParameterMap();for(String key : paramsMaps.keySet()) {String [] values = paramsMaps.get(key);String strVal = "";for(String str: values) {strVal = strVal + str + ", ";}System.out.println("key=" + key +", values=" + strVal);}

来获取。

这篇关于android设置打印机wifi(OKHttp+firefox的插件:HttpRequester+WireShake + servlet)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1000791

相关文章

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

PostgreSQL 默认隔离级别的设置

《PostgreSQL默认隔离级别的设置》PostgreSQL的默认事务隔离级别是读已提交,这是其事务处理系统的基础行为模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一 默认隔离级别概述1.1 默认设置1.2 各版本一致性二 读已提交的特性2.1 行为特征2.2

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

mtu设置多少网速最快? 路由器MTU设置最佳网速的技巧

《mtu设置多少网速最快?路由器MTU设置最佳网速的技巧》mtu设置多少网速最快?想要通过设置路由器mtu获得最佳网速,该怎么设置呢?下面我们就来看看路由器MTU设置最佳网速的技巧... 答:1500 MTU值指的是在网络传输中数据包的最大值,合理的设置MTU 值可以让网络更快!mtu设置可以优化不同的网

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。

全屋WiFi 7无死角! 华硕 RP-BE58无线信号放大器体验测评

《全屋WiFi7无死角!华硕RP-BE58无线信号放大器体验测评》家里网络总是有很多死角没有网,我决定入手一台支持Mesh组网的WiFi7路由系统以彻底解决网络覆盖问题,最终选择了一款功能非常... 自2023年WiFi 7技术标准(IEEE 802.11be)正式落地以来,这项第七代无线网络技术就以超高速

详解Linux中常见环境变量的特点与设置

《详解Linux中常见环境变量的特点与设置》环境变量是操作系统和用户设置的一些动态键值对,为运行的程序提供配置信息,理解环境变量对于系统管理、软件开发都很重要,下面小编就为大家详细介绍一下吧... 目录前言一、环境变量的概念二、常见的环境变量三、环境变量特点及其相关指令3.1 环境变量的全局性3.2、环境变