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

相关文章

Spring MVC如何设置响应

《SpringMVC如何设置响应》本文介绍了如何在Spring框架中设置响应,并通过不同的注解返回静态页面、HTML片段和JSON数据,此外,还讲解了如何设置响应的状态码和Header... 目录1. 返回静态页面1.1 Spring 默认扫描路径1.2 @RestController2. 返回 html2

SSID究竟是什么? WiFi网络名称及工作方式解析

《SSID究竟是什么?WiFi网络名称及工作方式解析》SID可以看作是无线网络的名称,类似于有线网络中的网络名称或者路由器的名称,在无线网络中,设备通过SSID来识别和连接到特定的无线网络... 当提到 Wi-Fi 网络时,就避不开「SSID」这个术语。简单来说,SSID 就是 Wi-Fi 网络的名称。比如

四种简单方法 轻松进入电脑主板 BIOS 或 UEFI 固件设置

《四种简单方法轻松进入电脑主板BIOS或UEFI固件设置》设置BIOS/UEFI是计算机维护和管理中的一项重要任务,它允许用户配置计算机的启动选项、硬件设置和其他关键参数,该怎么进入呢?下面... 随着计算机技术的发展,大多数主流 PC 和笔记本已经从传统 BIOS 转向了 UEFI 固件。很多时候,我们也

Linux中chmod权限设置方式

《Linux中chmod权限设置方式》本文介绍了Linux系统中文件和目录权限的设置方法,包括chmod、chown和chgrp命令的使用,以及权限模式和符号模式的详细说明,通过这些命令,用户可以灵活... 目录设置基本权限命令:chmod1、权限介绍2、chmod命令常见用法和示例3、文件权限详解4、ch

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

SpringBoot项目引入token设置方式

《SpringBoot项目引入token设置方式》本文详细介绍了JWT(JSONWebToken)的基本概念、结构、应用场景以及工作原理,通过动手实践,展示了如何在SpringBoot项目中实现JWT... 目录一. 先了解熟悉JWT(jsON Web Token)1. JSON Web Token是什么鬼

Servlet中配置和使用过滤器的步骤记录

《Servlet中配置和使用过滤器的步骤记录》:本文主要介绍在Servlet中配置和使用过滤器的方法,包括创建过滤器类、配置过滤器以及在Web应用中使用过滤器等步骤,文中通过代码介绍的非常详细,需... 目录创建过滤器类配置过滤器使用过滤器总结在Servlet中配置和使用过滤器主要包括创建过滤器类、配置过滤

使用Spring Cache时设置缓存键的注意事项详解

《使用SpringCache时设置缓存键的注意事项详解》在现代的Web应用中,缓存是提高系统性能和响应速度的重要手段之一,Spring框架提供了强大的缓存支持,通过​​@Cacheable​​、​​... 目录引言1. 缓存键的基本概念2. 默认缓存键生成器3. 自定义缓存键3.1 使用​​@Cacheab

IDEA常用插件之代码扫描SonarLint详解

《IDEA常用插件之代码扫描SonarLint详解》SonarLint是一款用于代码扫描的插件,可以帮助查找隐藏的bug,下载并安装插件后,右键点击项目并选择“Analyze”、“Analyzewit... 目录SonajavascriptrLint 查找隐藏的bug下载安装插件扫描代码查看结果总结Sona

java如何调用kettle设置变量和参数

《java如何调用kettle设置变量和参数》文章简要介绍了如何在Java中调用Kettle,并重点讨论了变量和参数的区别,以及在Java代码中如何正确设置和使用这些变量,避免覆盖Kettle中已设置... 目录Java调用kettle设置变量和参数java代码中变量会覆盖kettle里面设置的变量总结ja