驗證碼已是很是經常使用的反做弊、反攻擊手段了,其實要實現這個功能對技術水平好的人也不難,可是並非每一個人,每種語言都自然適合搞某個功能...不過咱們能夠經過封裝接口,來屏蔽差別化,把問題簡單化,如今就用一個簡單的nodejs腳原本實現驗證生成功能,例子以下:php
var http = require('http'); var qs = require('querystring'); //配置您申請的appKey和openId 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對應80端口,https 對應443端口,請使用者本身改正 var servlet="develop/verificationcode/get"; var method="get"; var request_url=domain+servlet; var params = {}; params['appKey']=app_key; params['openId']=open_id; //變更部分 params["w"]=200; params["h"]=50; params["len"]=7; params["fontSize"]=40; request_content(request_url,port,params,method); } main();
其餘語言的實現方式好比php,java,python等能夠點這裏查看,這裏暫且附上比較小衆的GO語言的實現方式:java
package main import ( "io/ioutil" "net/http" "net/url" "fmt" "strings" ) //配置您申請的appKey和openId 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 網絡請求 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("解析url錯誤:\r\n%v",err) return nil,err } //若是參數中有中文參數,這個方法會進行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 網絡請求 ,params 是url.Values類型 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 //初始化參數 params:=url.Values{} params.Set("appKey",APP_KEY) params.Set("openId",OPEN_ID) //變更部分 params.Set("w","200") params.Set("h","50") params.Set("len","7") params.Set("fontSize","40") //發送請求 data,err:=requestContent(requestUrl,params,method) fmt.Println(string(data)) if err!=nil{ fmt.Printf("解析url錯誤:\r\n%v",err) } }