吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2313|回复: 5
收起左侧

[其他转载] [C#] 联通查询话费 - C#源码

[复制链接]
真的小白 发表于 2021-10-5 12:36
本帖最后由 真的小白 于 2021-10-5 17:16 编辑

易语言原帖:https://www.52pojie.cn/thread-1522668-1-1.html

小白练习做了一个C#版本

[C#] 纯文本查看 复制代码
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace QueryTheBalanceOfUnicomPhoneNumber
{
    internal class Program
    {
        private static async Task Main(string[] args)
        {
            double balance;
            string phoneNumber;

            WindowHelper.SetConsoleCenterOfScreen();
            Console.Title = "联通手机号话费查询";

            while (true)
            {
                Console.Clear();
                Console.Write("请输入联通手机号码:");
                phoneNumber = Console.ReadLine();
                if (!Regex.IsMatch(phoneNumber, @"^1\d{10}$"))
                {
                    Console.WriteLine();
                    Console.WriteLine("手机号码输入有误,请重试!");
                    CmdHelper.Pause();
                    continue;
                }

                try
                {
                    balance = await UnicomHelper.GetBalanceByPhoneNumber(phoneNumber);
                    Console.WriteLine();

                    if (balance >= 0.0)
                    {
                        Console.Write("当前余额为:");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write($"{balance:0.00}");
                    }
                    else
                    {
                        Console.Write("当前欠费:");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write($"{Math.Abs(balance):0.00}");
                    }

                    Console.ResetColor();
                    Console.WriteLine("元");
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine();
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    CmdHelper.Pause();
                }
            }
        }
    }
}


[C#] 纯文本查看 复制代码
using System;
using System.Runtime.InteropServices;

namespace QueryTheBalanceOfUnicomPhoneNumber
{
    internal static class WindowHelper
    {
        private const string Kernel32 = "Kernel32.dll";
        private const string User32 = "User32.dll";
        private static readonly int ScreenWidth;
        private static readonly int ScreenHeight;
        private static readonly int SM_CXFULLSCREEN = 16;
        private static readonly int SM_CYFULLSCREEN = 17;

        static WindowHelper()
        {
            ScreenWidth = GetSystemMetrics(SM_CXFULLSCREEN);
            ScreenHeight = GetSystemMetrics(SM_CYFULLSCREEN);
        }

        internal static void SetConsoleCenterOfScreen()
        {
            IntPtr consoleHWnd = GetConsoleWindow();
            _ = GetWindowRect(consoleHWnd, out Rect consoleRect);
            int consoleWidth = consoleRect.Right - consoleRect.Left;
            int consoleHeight = consoleRect.Bottom - consoleRect.Top;
            _ = MoveWindow(consoleHWnd,
                           (ScreenWidth - consoleWidth) >> 1,
                           (ScreenHeight - consoleHeight) >> 1,
                           consoleWidth,
                           consoleHeight,
                           true);
        }

        [DllImport(Kernel32)]
        private static extern IntPtr GetConsoleWindow();

        [DllImport(User32)]
        private static extern int GetWindowRect(IntPtr hWnd, out Rect lpRect);

        [DllImport(User32)]
        private static extern int GetSystemMetrics(int nIndex);

        [DllImport(User32)]
        private static extern int MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, [MarshalAs(UnmanagedType.Bool)] bool bRepaint);

        private struct Rect
        {
            internal int Left;
            internal int Top;
            internal int Right;
            internal int Bottom;
        }
    }
}


[C#] 纯文本查看 复制代码
using System.Runtime.InteropServices;

namespace QueryTheBalanceOfUnicomPhoneNumber
{
    internal static class CmdHelper
    {
        private const string Msvcrt = "msvcrt.dll";
        private static readonly string PauseCommand = "pause";

        internal static void Pause()
        {
            _ = _wsystem(PauseCommand);
        }

        [DllImport(Msvcrt, CharSet = CharSet.Unicode)]
        private static extern int _wsystem(string str);
    }
}


[C#] 纯文本查看 复制代码
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;

namespace QueryTheBalanceOfUnicomPhoneNumber
{
    internal static class UnicomHelper
    {
        internal class UnicomPhoneNumber
        {
            public string phone { get; set; }
        }

        private static readonly HttpClient HttpClient = new HttpClient();
        private static readonly Uri PostUri = new Uri("https://weixin.10010js.com/app/charge/qryRealFee");
        private static readonly UnicomPhoneNumber PostPhoneNumber = new UnicomPhoneNumber();

        static UnicomHelper()
        {
            HttpClient.DefaultRequestHeaders.Referrer = new Uri("https://weixin.10010js.com/actPage/activity/index28.html?");
        }

        internal static async Task<double> GetBalanceByPhoneNumber(string phoneNumber)
        {
            PostPhoneNumber.phone = phoneNumber;
            HttpResponseMessage responseMessage = await HttpClient.PostAsync(PostUri, JsonContent.Create(PostPhoneNumber));
            responseMessage.EnsureSuccessStatusCode();
            string responseString = await responseMessage.Content.ReadAsStringAsync();
            if (string.IsNullOrEmpty(responseString))
            {
                throw new ArgumentException("请输入正确的联通手机号码!");
            }
            return Math.Round(double.Parse(responseString) / 100.0, 2);
        }
    }
}

免费评分

参与人数 1吾爱币 +3 热心值 +1 收起 理由
苏紫方璇 + 3 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

述说过去 发表于 2021-10-5 17:27
接口黄了
 楼主| 真的小白 发表于 2021-10-5 17:30
述说过去 发表于 2021-10-5 17:31
真的小白 发表于 2021-10-5 17:30
这也太快了,刚刚修改出来,就当一次练习了

中午刚给APP添加一个小工具 现在又要取消了 哈哈
 楼主| 真的小白 发表于 2021-10-5 17:37
述说过去 发表于 2021-10-5 17:31
中午刚给APP添加一个小工具 现在又要取消了 哈哈

我图标都找好了,准备分发吹牛使用。。。
QQ截图20211005173516.png
Judas 发表于 2021-10-5 22:56
各种语言的版本都要齐了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 14:51

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表