Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增C#版本demo #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ fastlane/Preview.html
fastlane/screenshots
fastlane/test_output

/C#/CSharpDemo/bin/*
/C#/CSharpDemo/obj/*
/C#/CSharpDemo/.vs/CSharpDemo/v16/TestStore/0/*.testlog
/C#/CSharpDemo/.vs/CSharpDemo/v16/*.suo
/C#/CSharpDemo/.vs/CSharpDemo/v16/TestStore/0/*.manifest
25 changes: 25 additions & 0 deletions C#/CSharpDemo.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31025.194
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpDemo", "CSharpDemo\CSharpDemo.csproj", "{1F904FCB-7180-4388-BDD0-457D56A988AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1F904FCB-7180-4388-BDD0-457D56A988AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F904FCB-7180-4388-BDD0-457D56A988AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F904FCB-7180-4388-BDD0-457D56A988AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F904FCB-7180-4388-BDD0-457D56A988AC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1475C0AB-18D7-4EE1-ADF7-329BAE45C03B}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions C#/CSharpDemo/CSharpDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RestSharp" Version="106.11.7" />
</ItemGroup>

</Project>
133 changes: 133 additions & 0 deletions C#/CSharpDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using Newtonsoft.Json.Linq;
using RestSharp;

namespace CSharpDemo
{
public class Program
{
public static RestClient Client { get; set; } = new RestClient("https://api.seniverse.com/");
/// <summary>
/// 在这里填入你的密钥
/// </summary>
public static string PrivateKey { get; set; } = "your PrivateKey";
/// <summary>
/// 在这里填入你的公钥
/// </summary>
public static string PublicKey { get; set; } = "your PublicKey";
public static string Api { get; set; } = "v3/weather/daily.json";
public static string Location { get; set; } = "beijing";
public static string Language { get; set; } = "zh-Hans";
public static string Unit { get; set; } = "c";
public static string Start { get; set; } = "0";
public static string Days { get; set; } = "2";
public static string TimeStamp { get; set; }

/// <summary>
/// 签名失效时间
/// </summary>
public static string ttl { get; set; } = "1800";
static void Main(string[] args)
{
var response = GetWeatherInfoBySign();
//解析需要的数据
var weatherModels = JObject.Parse(response).SelectToken("results").First.
SelectToken("daily").ToObject<List<WeatherModel>>();
}
/// <summary>
/// 用私钥获取天气信息
/// </summary>
/// <returns></returns>
private static string GetWeatherInfo()
{
var request = BuildRestRequest();
request.AddQueryParameter("key", PrivateKey);
var response = Client.Execute(request).Content;
return response;
}
/// <summary>
/// 通过签名获取天气信息
/// </summary>
/// <returns></returns>
private static string GetWeatherInfoBySign()
{
var request = BuildRestRequest();
request.AddQueryParameter("sig", CreateSign());
request.AddQueryParameter("ts", TimeStamp);
request.AddQueryParameter("ttl", ttl);
request.AddQueryParameter("uid", PublicKey);

var response = Client.Execute(request).Content;
return response;
}

private static RestRequest BuildRestRequest()
{
RestRequest request = new RestRequest(Api);

request.AddQueryParameter("location", Location);
request.AddQueryParameter("language", Language);
request.AddQueryParameter("unit", Unit);
request.AddQueryParameter("start", Start);
request.AddQueryParameter("days", Days);
return request;
}
/// <summary>
/// 构造签名
/// </summary>
/// <returns></returns>
public static string CreateSign()
{
TimeStamp = GetTimeStamp();
string queryString = $"ts={TimeStamp}&ttl={ttl}&uid={PublicKey}";
return HMACSHA1Text(queryString, PrivateKey);
}
/// <summary>
/// 获取时间戳
/// </summary>
/// <param name="bflag">false时是13位</param>
/// <returns></returns>
public static string GetTimeStamp(bool bflag=true)
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
string ret = string.Empty;
if (bflag)
ret = Convert.ToInt64(ts.TotalSeconds).ToString();
else
ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();

return ret;
}
/// <summary>
/// HMACSHA1加密,返回base64
/// </summary>
/// <param name="text">要加密的原串</param>
///<param name="key">私钥</param>
/// <returns></returns>
public static string HMACSHA1Text(string text, string key)
{
//HMACSHA1加密
HMACSHA1 hmacsha1 = new HMACSHA1();
hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(key);

byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(text);
byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);

return Convert.ToBase64String(hashBytes);

}
}
/// <summary>
/// 在这里我只解析了关心的数据,大家可按需要把字段都解析出来
/// </summary>

public class WeatherModel
{
public DateTime date { get; set; }
public string text_day { get; set; }
public double high { get; set; }
public double low { get; set; }
}
}