1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Web; using System.Net; using System.IO; using System.Text.RegularExpressions; using System.Security.Cryptography; namespace Test1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click( object sender, RoutedEventArgs e) { var mainUrl = @"http://api.wap.guoguo-app.com/h5/mtop.cnwireless.cnlogisticdetailservice.wapquerylogisticpackagebymailno/1.0/" ; var cookieUrl = @"http://api.wap.guoguo-app.com/h5/mtop.cnwireless.cncainiaoappservice.getlogisticscompanylist/1.0/?v=1.0&api=mtop.cnwireless.CNCainiaoAppService.getLogisticsCompanyList&appKey=12574478&t=1496741493353&callback=mtopjsonp1&type=jsonp&sign=b0f7376271effd90e311f998ad3a3efb&data=%7B%22version%22%3A0%2C%22cptype%22%3A%22all%22%7D" ; var mailNo = "826060403" ; //运单号码 //准备参数 var tikets = (DateTime.Now - Convert.ToDateTime( "1970-01-01 00:00:00" )).Ticks.ToString(); var cookie = GetCookie(cookieUrl); //先请求一次获得cookie,可以先缓存下来。 MessageBox.Show(cookie); var key = new Regex( @"(?:^|;\s*)_m_h5_tk\=([^;]+)(?:;\s*|$)" ).Match(cookie).Value.Split( \'=\' )[1] .Split( \'_\' )[0]; var sign = GetMD5Hash(key + "&" + tikets + "&12574478" + "&{\"mailNo\":\"" + mailNo + "\"}" ) .ToLower(); //获得参数列表 var urlParas = string .Format( "?v=1.0&api=mtop.cnwireless.CNLogisticDetailService.wapqueryLogisticPackageByMailNo&appKey=12574478&t={0}&callback=mtopjsonp&type=jsonp&sign={1}" , tikets, sign); urlParas += "&data={\"mailNo\":\"" + mailNo + "\"}" ; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(mainUrl + urlParas); request.Method = "GET" ; request.Headers.Add( "Cookie" , cookie); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string content = reader.ReadToEnd(); Console.WriteLine(content); MessageBox.Show(content); } //md5加密 public static string GetMD5Hash(String input) { // Use input string to calculate MD5 hash MD5 md5 = System.Security.Cryptography.MD5.Create(); byte [] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); byte [] hashBytes = md5.ComputeHash(inputBytes); // Convert the byte array to hexadecimal string StringBuilder sb = new StringBuilder(); for ( int i = 0; i < hashBytes.Length; i++) { sb.Append(hashBytes[i].ToString( "x2" )); // To force the hex string to lower-case letters instead of // upper-case, use he following line instead: // sb.Append(hashBytes[i].ToString("x2")); } return sb.ToString(); // return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(input, "md5"); } //获得cookie public static string GetCookie( string url, int Timeout = 5000, bool isNeedProxy = true ) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET" ; request.AllowAutoRedirect = false ; request.ContentType = "application/x-www-form-urlencoded;charset=gbk" ; request.CookieContainer = new CookieContainer(); request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11" ; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default); string content = reader.ReadToEnd(); return response.Headers.Get( "Set-Cookie" ); } catch (Exception ex) { return null ; } } } } |
请发表评论