受欢迎的博客标签

微信小程序-自建小程序服务器 c# 服务器搭建

Published

 

1.域名(备案过的)
2.https证书
3.配置本地的nginx
4.本地安装server 服务器

 

https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html

    •  
文章目录

一、注册微信小程序

进入微信公众平台,使用邮箱账号,选择注册小程序账号。一个邮箱只能注册一个类型的微信账号(订阅号、服务号、小程序)。

二、搭建Https服务器

1、域名注册

  • 1.1、登录腾讯云域名注册页面,并注册账号。

2、SSL证书管理

  • 2.1、选择“云产品”—”域名服务“—”SSL证书管理",选择“申请证书(免费)”。若未认证,系统将提示进入系统认证

  • 2.2、个人认证。

  • 2.3、域名验证→域名验证指南

  • 2.4、下载SSL证书。用于服务配置

3、服务器域名配置

      

  • 3.1、小程序端只能发起HTTPS请求;且只能使用配置服务器域名上面绑定的域名,所以在开发小程序前,一定要配置好域名。
  • 3.2、修改conf下的server.xml文件(先复制一份server.xml,防止修改内容出错)。修改内容如下:
    a、将Http端口设置为默认的80端口,重定向端口设置为443(小程序服务器仅支持以上两个端口)。
    b、小程序端发起HTTPS请求如下:
Page({
  onReady: function () {
    wx.request({
      url: 'https://weixin.techeek.cn/', //仅为示例,并非真实的接口地址
    })
  },
})
  •  

4、搭建第一个Https web server应用

  • 4.1、.Net 创建一个应用,新增如下代码:
  • using Microsoft.AspNetCore.DataProtection;
    using Microsoft.AspNetCore.Mvc;
    using System.Diagnostics;
    using System.Net;
    using System.Text;
    
    using Newtonsoft.Json;
    
    using WeiXinThirdserver_Miniprogrom_nocloud.Models;
    
    namespace WeiXinThirdserver_Miniprogrom_nocloud.Controllers
    {
        public class HomeController : Controller
        {
            private readonly ILogger<HomeController> _logger;
    
            public HomeController(ILogger<HomeController> logger)
            {
                _logger = logger;
            }
    
            public IActionResult Index()
            {
                string appid = "wxae737f";
                string secret = "c5c88552529";
                string code = "0a1LIq0w3Dra923GWq1w3fWI1z2LIq0O";
    
                //AppID(小程序ID)	wxae AppSecret(小程序密钥) c5c88552529
    
                string cod = code;
                string html = string.Empty;
                string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + cod + "&grant_type=authorization_code";
    
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                Stream ioStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(ioStream, Encoding.UTF8);
                html = sr.ReadToEnd();
                sr.Close();
                ioStream.Close();
                response.Close();
                RepParamrep rep = JsonConvert.DeserializeObject<RepParamrep>(html);
               
    
    
                return View();
            }
    
            public IActionResult Privacy()
            {
                return View();
            }
    
            [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
            public IActionResult Error()
            {
                return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
            }
        }
    
    
        public class RepParamrep
        {
            public string session_key { get; set; }
            public string openid { get; set; }
        }
    }
    

 

  • 1
  • 4.2、启动项目,本文使用Spring简单的向输出流输出字符串,用于小程序的接受。
@RequestMapping("/test") @ResponseBody public String test(){ return "hehe"; }
  • 1
  • 4.3、通过腾讯云注册的域名访问相应Servlet,Http请求将自动转入Https请求

三、微信小程序访问服务器,并获取返回值

1、服务器配置

  • 1.1、进入微信公众平台,并用小程序进行登录,选择“设置”—“开发设置”。在“服务器域名中,选择修改域名,填入所申请的域名。如下图:
  • 1.2、小程序中js调用微信接口,并访问服务器,获取返回值
 

自此,小程序成功通过https的方式与服务器进入连接。

 
 
 
Useful links
 
https://www.cnblogs.com/nfmc/p/17242938.html