[Fertilizer] Come home to capture guide I wrote a small note of it!

### Foreword

    Some time ago to buy a machine from a younger sister before cooing there to back (fog), ready for the printer to do a stand-alone expansion, according to the official gives the interface to extend it into one that will allow many users to send me small online printer paper.

 

 

 

    After using the interface morning sent a group of friends, then today I received the content ...

 

### of the Constitution

 

### sent three times Alipay account

 

### mysterious link

 

### told me to play games (my own hair)

 

### code is also very tired one day knock

 

  It feels like passing notes among junior secretly desk as warm.

 

    but!

 

    Print constitution is really a waste of paper, every minute roll of thermal paper is gone, although thermal paper is cheap, but a long or difficult to set down the Constitution.

    Therefore, in order to prevent people from printing the Constitution, textbooks, periodic table, for Students, Three Character Classic and things like that, I ... did not do anything, just the POST request interface is replaced by a GET request. So the browser can automatically help me too long GET request to pass out! Unless the old six without a browser, using this tool to fidder request.

 

    Before posting length resolved, there is a question about sending anonymous, although I provide a contact name or leave the box, but this is not mandatory, so there is no access to log on Weibo / QQ and other third parties also temporarily not beta.

 

    Interested public can directly reply [No.] gugu address to get tested, but do not engage in a statement .... bells and whistles.

 

    If you goo interested in developing goo-machine interface, you can continue to browse the content below!

 

### interface development documentation

 

    If you want to goo goo machine interface development, you need to apply in their official website developers ak.

    http://open.memobird.cn/

    

    After the application can be encapsulated by the official API to use, mainly used API has two. It is a binding request, a print request.

 

1) Binding Request / setuserbind

    我们需要发送ak,memobirdID(设备号),useridentifying(用户Id)。之后会返回与服务器关联的userId。我们需要使用这个userId来进行打印。

 

    用fidder请求如下:

   为了用代码实现,我们先封装一个Http请求发起的方法。

 1    private static String sendRequest(String url) {
 2         URL u ;
 3         HttpURLConnection httpURLConnection ;
 4         BufferedReader bf ;
 5         String response = "" ;
 6         String readLine  ;
 7         try{
 8             u = new URL(url) ;
 9             httpURLConnection = (HttpURLConnection)u.openConnection() ;
10             int responsecode = httpURLConnection.getResponseCode() ;  // 返回码
11             if(responsecode==200) {
12                 bf = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
13                 while ((readLine = bf.readLine()) != null) {
14                     response += readLine += "\r\n";
15                 }
16             }else{
17                 System.out.println("NOT  "+responsecode);
18             }
19         }catch(Exception e){
20             System.out.println("Exception->"+e);
21         }
22         return response ;
23     }
24

    这个方法最后返回请求体。

    当我们绑定用户时,只需要调用这个方法。

1   public static String setUserBind(){
2         String url = BASE_URL + "/setuserbind?ak=" + ak + "&timestamp=2019-08-31%14:22:39&memobirdID=" + memobirdID  + "&useridentifying=" + useridentifying;
3         return sendRequest(url) ; 
4     }

2)打印请求 printpaper

    这个请求需要的参数是ak,memobirdID(设备号),需要打印的内容,还有第一步绑定时返回的userId。

1     public static String printPaper(String content,String userId,String time) {
2         String url = BASE_URL + "/printpaper?ak=" + ak + "&timestamp="+time+"&printcontent=T:" + content + "&memobirdID=" + memobirdID + "&userId=" + userId ;
3         return sendRequest(url);
4     }

    fidder请求如下,printcontentid为本次打印的id编号。

    这样就成功了!

 

    至于是否已经打印的接口,可以去咕机开发者文档中自行查看学习。

 

### 发送内容编码

 

    ​有一个问题是,发送打印的数据需要在GBK编码的基础上进行base64加密,所以我们自己封装还要有加密过程。

1    public static String toBase64(String str){
2         try{
3             byte[] encodeBase64 = Base64.encodeBase64(str.getBytes("GBK"));
4             return new String(encodeBase64);
5         } catch(UnsupportedEncodingException e){
6             e.printStackTrace();
7         }
8         return "";
9     }

    完成了这些关键的封装,我们只需要再搭建一个简单的页面,就可以了,使用最简单的servlet+jsp就可以了,用一个表单把内容发送到后台,然后调用后台来调用接口完成打印。

1 <form class="center-block" action="send" method="get">
2     Name:
3     <input type="text" class="form-control" placeholder="您的姓名或联系方式" name="name">
4     <br>
5     Content:
6     <textarea class="form-control" rows="6" placeholder="需要发送的内容" name="content"></textarea>
7     <br><br>
8     <input type="submit" style="margin: 0px auto;display: table;" class="btn btn-primary btn-lg btn-block" value="Submit">
9 </form>

 

Guess you like

Origin www.cnblogs.com/LexMoon/p/gugu.html
Recommended