C# 后台获取API接口数据

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Net;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace ClientTest
10 {
11     class BasicClient
12     {
13         public string SendRequest(string url, string method, string auth, string reqParams)
14         {
15             //这是发送Http请求的函数,可根据自己内部的写法改造
16             HttpWebRequest myReq = null;
17             HttpWebResponse response = null;
18             string result = string.Empty;
19             try
20             {
21                 myReq = (HttpWebRequest)WebRequest.Create(url);
22                 myReq.Method = method;
23                 myReq.ContentType = "application/json;";
24                 myReq.KeepAlive = false;
25 
26                 //basic 验证下面这句话不能少
27                 if (!String.IsNullOrEmpty(auth))
28                 {
29                     myReq.Headers.Add("Authorization", "Basic " + auth);
30                 }
31 
32                 if (method == "POST" || method == "PUT")
33                 {
34                     byte[] bs = Encoding.UTF8.GetBytes(reqParams);
35                     myReq.ContentLength = bs.Length;
36                     using (Stream reqStream = myReq.GetRequestStream())
37                     {
38                         reqStream.Write(bs, 0, bs.Length);
39                         reqStream.Close();
40                     }
41                 }
42 
43                 response = (HttpWebResponse)myReq.GetResponse();
44                 HttpStatusCode statusCode = response.StatusCode;
45                 if (Equals(response.StatusCode, HttpStatusCode.OK))
46                 {
47                     using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
48                     {
49                         result = reader.ReadToEnd();
50                     }
51                 }
52             }
53             catch (WebException e)
54             {
55                 if (e.Status == WebExceptionStatus.ProtocolError)
56                 {
57                     HttpStatusCode errorCode = ((HttpWebResponse)e.Response).StatusCode;
58                     string statusDescription = ((HttpWebResponse)e.Response).StatusDescription;
59                     using (StreamReader sr = new StreamReader(((HttpWebResponse)e.Response).GetResponseStream(), Encoding.UTF8))
60                     {
61                         result = sr.ReadToEnd();
62                     }
63                 }
64                 else
65                 {
66                     result = e.Message;
67                 }
68             }
69             finally
70             {
71                 if (response != null)
72                 {
73                     response.Close();
74                 }
75                 if (myReq != null)
76                 {
77                     myReq.Abort();
78                 }
79             }
80 
81             return result;
82         }
83 
84         public string sendHttpRequest(string url, string reqparam, string method = "POST")
85         {
86             string auth = Base64Encode("key:secret");
87             return SendRequest(url, method, auth, reqparam);
88         }
89 
90         private string Base64Encode(string value)
91         {
92             byte[] bytes = Encoding.Default.GetBytes(value);
93             return Convert.ToBase64String(bytes);
94         }
95     }
96 }