using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; namespace 淘宝网址解析 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static Dictionary 淘宝网站 = new Dictionary { ["淘宝网页版"] = "item.taobao.com", ["淘宝手机版"] = "m.taobao.com", ["淘宝手机版new"] = "new.m.taobao.com", ["淘宝短网址"] = "m.tb.cn", ["天猫网址"] = "detail.tmall.com", ["天猫超市"] = "chaoshi.detail.tmall.com", }; BindingList<网址参数> 参数列表 = new(); private async void button1_ClickAsync(object sender, EventArgs e) { //清空参数列表 参数列表.Clear(); string temp = ""; //取出完整网址 try { temp = textBox1.Text.Substring(textBox1.Text.IndexOf("http")); } catch (Exception) { MessageBox.Show("请输入正确的网址,例如https://detail.tmall.com/item.htm?id=640330331740"); return; } temp = temp.Split(' ')[0]; //解析链接 var 网页域名 = ""; var 网站页面 = ""; try { 网页域名 = temp.Split('/')[2]; 网站页面 = temp.Split('/')[3].Split('?')[0]; } catch (Exception) { MessageBox.Show("网址可能不是太正确..."); return; } if (网页域名 == 淘宝网站["淘宝短网址"]) { //解析短网址 temp = await 解析短网址(temp); temp = temp.Substring(textBox1.Text.IndexOf("http")); temp = temp.Split(' ')[0]; } if (checkBox1.Checked) if (网页域名 == 淘宝网站["淘宝手机版new"] || 网页域名 == 淘宝网站["淘宝手机版"]) { 网页域名 = 淘宝网站["淘宝网页版"]; 网站页面 = @"item.htm"; } var 结果网址 = $"https://{网页域名}/{网站页面}?"; temp = temp.Substring(temp.IndexOf("htm?") + 4); var 参数 = temp.Split("&"); bool 找到id = false; foreach (var item in 参数) { var 参数与内容 = item.Split("="); 参数列表.Add(new 网址参数 { 参数名称 = 参数与内容[0], 参数内容 = 参数与内容[1] }); if (参数与内容[0] == "id") { 结果网址 += $"{参数与内容[0]}={参数与内容[1]}"; 找到id = true; } } if(找到id == false) { MessageBox.Show("未找到宝贝id!结果大概率是寄了","可恶!"); } dataGridView1.DataSource = 参数列表; dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; textBox2.Text = 结果网址; if (checkBox2.Checked) { Process.Start(new ProcessStartInfo() { FileName = 结果网址, UseShellExecute = true }); } } private static HttpClient http客户端 = new(); private async Task 解析短网址(string 网址) { try { HttpResponseMessage response = await http客户端.GetAsync(网址); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); // Above three lines can be replaced with new helper method below // string responseBody = await client.GetStringAsync(uri); var aa = responseBody.Substring(responseBody.IndexOf(@"var url = 'https://new.m.taobao.com")); aa = aa.Substring(0, aa.IndexOf('\'', aa.IndexOf('\'') + 1)); aa = aa.Replace(@"var url = '", "").Replace(@"'", ""); return aa; } catch (HttpRequestException e) { Debug.WriteLine("\nException Caught!"); Debug.WriteLine("Message :{0} ", e.Message); return null; } } public class 网址参数 { public string 参数名称 { get; set; } public string 参数内容 { get; set; } } private void button2_Click(object sender, EventArgs e) { http客户端.BaseAddress = new Uri($"https://{淘宝网站["淘宝短网址"]}"); } } }