zoukankan      html  css  js  c++  java
  • 简单的几句接口调用,完美完成缩短网站

    短网址有时候确实是非常刚需的一个需求,网上这类网站是很多的,但是有时候想调用接口来缩短网址,很多这类网站却没有提供。网上常用的接口比如新浪的短网址接口,小葱计算的短网址接口,其实都很好用,现在以小葱计算的短网址接口为例,用nodejs来展示如何使用:

    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="life/shorturl/get";
    	var method="get";
    	var request_url=domain+servlet;
    
    	var params = {}; 
    	params['appKey']=app_key;
        params['openId']=open_id;
    	
    	//变动部分
    	params["url"]="https://www.baidu.com/";
    	
    	request_content(request_url,port,params,method);
    }
    
    main();
    

    当然go语言的实现方式也挺简单的,如下:

    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错误:
    %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 :="life/shorturl/get"
    	method :="get"
    	requestUrl:=domain+servlet
    	
        //初始化参数
        params:=url.Values{}
    	
    	params.Set("appKey",APP_KEY)
    	params.Set("openId",OPEN_ID)
    	
    	//变动部分
    	params.Set("url","https://www.baidu.com/")
     
     
        //发送请求
        data,err:=requestContent(requestUrl,params,method)
        fmt.Println(string(data))
    	if err!=nil{
            fmt.Printf("解析url错误:
    %v",err)
        }
    }	
    

    调用api实现,没有太大的技术复杂度,感谢阅读。

  • 相关阅读:
    Docker大会的新福利:LinuxKit 和 Moby 开源项目
    NS3
    (OK) NS3
    MPTCP
    Utilizing multi-core processors in NS-2
    (OK) Fedora 24
    error compiling gcc: undefined reference to libc_name_p
    gccxml
    NS3
    NS3
  • 原文地址:https://www.cnblogs.com/huangxie/p/11649141.html
Copyright © 2011-2022 走看看