最好用的内网穿透!开发微信

神卓互联它可以将局域网内的设备暴露在公网上,让外部设备可以访问到内网设备,从而实现内网穿透。

神卓互联内网穿透的步骤:
下载并安装神卓互联客户端和服务器端。
在服务器端创建一个应用,并设置端口号和密码。
在客户端中添加要穿透的设备,包括设备的名称、内网IP地址和端口号。
点击“启动连接”,客户端会与服务器端建立连接

在服务器端中可以查看到客户端的连接情况,如果连接成功,则可以在服务器端上看到客户端添加的设备信息。

在客户端中可以看到已经穿透的设备,可以通过神卓互联提供的公网地址和端口号来访问这些设备。

需要注意的是,神卓互联无需在防火墙中打开端口,才能让外部设备访问到内网设备。

在这里插入图片描述

神卓互联 Java 开发微信接口的示例代码:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blade.kit.StringKit;
import com.blade.kit.http.HttpRequest;
import com.blade.kit.http.HttpResponse;
import com.blade.kit.json.JsonObject;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.Route;
import com.blade.mvc.ui.RestResponse;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.qiniu.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Path
public class WechatController {

    private String appid = "your appid";
    private String appsecret = "your appsecret";
    private String token = "your token";
    private String encodingAESKey = "your encodingAESKey";

    @Route(value = "/wechat", method = HttpMethod.POST)
    public RestResponse<String> wechat() {
        String signature = request.query("signature");
        String timestamp = request.query("timestamp");
        String nonce = request.query("nonce");
        String echostr = request.query("echostr");

        if (StringUtils.isNullOrEmpty(signature) || StringUtils.isNullOrEmpty(timestamp) || StringUtils.isNullOrEmpty(nonce)) {
            return RestResponse.fail("Illegal request");
        }

        String[] arr = {token, timestamp, nonce};
        Arrays.sort(arr);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);
        }
        String mySignature = SHA1.encode(sb.toString());

        if (!mySignature.equals(signature)) {
            return RestResponse.fail("Illegal request");
        }

        if (StringUtils.isNullOrEmpty(echostr)) {
            try {
                String xml = getRequestString(request);
                JSONObject json = XML.toJSONObject(xml);
                String fromUserName = json.getJSONObject("xml").getString("FromUserName");
                String toUserName = json.getJSONObject("xml").getString("ToUserName");
                String msgType = json.getJSONObject("xml").getString("MsgType");
                String content = json.getJSONObject("xml").getString("Content");

                String response = "";
                if (msgType.equals("text")) {
                    response = "你发送的是文本消息";
                } else if (msgType.equals("image")) {
                    response = "你发送的是图片消息";
                } else if (msgType.equals("voice")) {
                    response = "你发送的是语音消息";
                } else if (msgType.equals("video")) {
                    response = "你发送的是视频消息";
                } else if (msgType.equals("shortvideo")) {
                    response = "你发送的是小视频消息";
                } else if (msgType.equals("location")) {
                    response = "你发送的是地理位置消息";
                } else if (msgType.equals("link")) {
                    response = "你发送的是链接消息";
                } else if (msgType.equals("event")) {
                    String eventType = json.getJSONObject("xml").getString("Event");
                    if (eventType.equals("subscribe")) {
                        response = "感谢您关注我们的公众号!";
                    } else if (eventType.equals("unsubscribe")) {
                        response = "取消关注成功!";
                    }
                }
                return RestResponse.ok(response);
            } catch (Exception e) {
                e.printStackTrace();
                return RestResponse.fail("Failed to handle message");
            }
        } else {
            return RestResponse.ok(echostr);
        }
    }

    private String getRequestString(HttpRequest request) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader

        reader = request.getReader();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}

private String getToken() {
    String accessToken = "";
    String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret;
    HttpResponse response = HttpRequest.get(url).send();
    String result = response.body();
    if (!StringKit.isBlank(result)) {
        JSONObject json = JSON.parseObject(result);
        accessToken = json.getString("access_token");
    }
    return accessToken;
}

private String getJsapiTicket() {
    String jsapiTicket = "";
    String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=" + getToken();
    HttpResponse response = HttpRequest.get(url).send();
    String result = response.body();
    if (!StringKit.isBlank(result)) {
        JSONObject json = JSON.parseObject(result);
        jsapiTicket = json.getString("ticket");
    }
    return jsapiTicket;
}

private String getNonceStr() {
    return UUID.randomUUID().toString().replace("-", "").substring(0, 16);
}

private String getTimestamp() {
    return Long.toString(System.currentTimeMillis() / 1000);
}

private String getSignature(String noncestr, String jsapiTicket, String timestamp, String url) {
    String str = "jsapi_ticket=" + jsapiTicket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;
    return SHA1.encode(str);
}

@Route(value = "/wxconfig", method = HttpMethod.GET)
public RestResponse<JsonObject> wxconfig() {
    String jsapiTicket = getJsapiTicket();
    String noncestr = getNonceStr();
    String timestamp = getTimestamp();
    String url = request.query("url");
    String signature = getSignature(noncestr, jsapiTicket, timestamp, url);

    JsonObject jsonObject = new JsonObject();
    jsonObject.add("appId", appid);
    jsonObject.add("nonceStr", noncestr);
    jsonObject.add("timestamp", timestamp);
    jsonObject.add("signature", signature);

    return RestResponse.ok(jsonObject);
}

未完待续!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值