Noejs Language Implements Verification Code Generation Function

Keywords: Go network PHP Java Python

Verification code has been a very common anti-cheating and anti-attack means, in fact, it is not difficult for people with good technical level to achieve this function, but not everyone, every language is naturally suitable for a function... But we can shield differentiation by encapsulating interfaces, simplify the problem, and now use a simple nodejs script to achieve verification generation function. Examples are as follows:

var http = require('http'); 
var qs = require('querystring'); 

//Configure the appKey and openId you applied for
app_key = "***";
open_id = "***";
 
function request_content(request_url,port,params,method){
    
    var path=request_url;
    if(!!params){
        var content = qs.stringify(params);  
        path=request_url+'?' + content;
    }
    
    var options = { 
        port: port,
        path: path,
        method: method
    }; 
    
    if(method.toLowerCase()=='post'){
        options['headers']="Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8";
    }

    var req = http.request(options, function (res) { 
        res.setEncoding('utf8'); 
        res.on('data', function (chunk) { 
            console.log(chunk); 
        }); 
    });  
    
    req.on('error', function (e) { 
        console.log('problem with request: ' + e.message); 
    }); 
    
    req.end();
}

function main(){

    var domain="http://api.xiaocongjisuan.com/";
    var port=8080;//http corresponds to 80 ports, https corresponds to 443 ports, please users correct themselves
    var servlet="develop/verificationcode/get";
    var method="get";
    var request_url=domain+servlet;

    var params = {}; 
    params['appKey']=app_key;
    params['openId']=open_id;
    
    //Variable part
    params["w"]=200;
    params["h"]=50;
    params["len"]=7;
    params["fontSize"]=40;
    
    request_content(request_url,port,params,method);
}

main();

Other language implementations such as php, java, python, etc. Click here to see For the time being, the implementation of GO language in a relatively small group is attached.

package main

import (
    "io/ioutil"
    "net/http"
    "net/url"
    "fmt"
    "strings"
)
 
//Configure the appKey and openId you applied for
const APP_KEY ="yours";
const OPEN_ID ="yours";

func requestContent(requestUrl string,params url.Values,method string)(rs[]byte ,err error){
    
    if strings.ToUpper(method)=="GET"{
        return get(requestUrl,params)
    }
    return post(requestUrl,params)
}

// get network request
func get(requestUrl string,params url.Values)(rs[]byte ,err error){
    var Url *url.URL
    Url,err=url.Parse(requestUrl)
    if err!=nil{
        fmt.Printf("analysis url error:\r\n%v",err)
        return nil,err
    }
    //If there are Chinese parameters in the parameters, this method will do URLEncode.
    Url.RawQuery=params.Encode()
    resp,err:=http.Get(Url.String())
    if err!=nil{
        fmt.Println("err:",err)
        return nil,err
    }
    defer resp.Body.Close()
    return ioutil.ReadAll(resp.Body)
}
 
// post network request, params is url.Values type
func post(requestUrl string, params url.Values)(rs[]byte,err error){
    resp,err:=http.PostForm(requestUrl, params)
    if err!=nil{
        return nil ,err
    }
    defer resp.Body.Close()
    return ioutil.ReadAll(resp.Body)
}

func main(){

    domain :="http://api.xiaocongjisuan.com/"

    servlet :="develop/verificationcode/get"
    method :="get"
    requestUrl:=domain+servlet
    
    //Initialization parameters
    params:=url.Values{}
    
    params.Set("appKey",APP_KEY)
    params.Set("openId",OPEN_ID)
    
    //Variable part
    params.Set("w","200")
    params.Set("h","50")
    params.Set("len","7")
    params.Set("fontSize","40")
 
 
    //Send request
    data,err:=requestContent(requestUrl,params,method)
    fmt.Println(string(data))
    if err!=nil{
        fmt.Printf("analysis url error:\r\n%v",err)
    }
}    

Posted by eddy556 on Thu, 10 Oct 2019 07:48:40 -0700