王 垚
2021-11-12 0ce538e3c3dc01153ce1bfc2d75276881206c222
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
 
namespace ZD.Share.Common
{
    public class HttpHelper
    {
        //public static string PostData(string url, string postData)
        //{
        //    ASCIIEncoding encoding = new ASCIIEncoding();
        //    byte[] data = Encoding.UTF8.GetBytes(postData);
        //    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
 
        //    myRequest.Method = "POST";
        //    myRequest.ContentType = "application/x-www-form-urlencoded";
        //    myRequest.ContentLength = data.Length;
        //    Stream newStream = myRequest.GetRequestStream();
 
        //    newStream.Write(data, 0, data.Length);
        //    newStream.Close();
 
        //    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
        //    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
        //    string content = reader.ReadToEnd();
        //    reader.Close();
        //    return content;
        //}
 
        //public static string GetData(string url)
        //{
        //    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
        //    myRequest.Method = "GET";
        //    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
        //    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
        //    string content = reader.ReadToEnd();
        //    reader.Close();
        //    return content;
        //}
 
        public static string HttpPostJson(string url, Dictionary<string, string> keyValues)
        {
 
            HttpClient httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
            var body = new FormUrlEncodedContent(keyValues);
            var response = httpClient.PostAsync(url, body).Result;
            var data = response.Content.ReadAsStringAsync().Result;
            return data;
        }
 
        public static string PostAsyncJson(string url, string json)
        {
            HttpClient client = new HttpClient();
            HttpContent content = new StringContent(json);
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            HttpResponseMessage response = client.PostAsync(url, content).Result;
            string responseBody = response.Content.ReadAsStringAsync().Result;
            return responseBody;
        }
 
        ///// <summary>
        ///// FORM表单POST方式上传一个多媒体文件
        ///// </summary>
        ///// <param name="url">API URL</param>
        ///// <param name="typeName"></param>
        ///// <param name="fileName"></param>
        ///// <param name="fs"></param>
        ///// <param name="encoding"></param>
        ///// <returns></returns>
        //public static string HttpRequestPost(string url, string typeName, string fileName, Stream fs, string encoding = "UTF-8")
        //{
        //    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        //    request.Method = "POST";
        //    request.Timeout = 10000;
        //    var postStream = new MemoryStream();
        //    #region 处理Form表单文件上传
        //    //通过表单上传文件
        //    string boundary = "----" + DateTime.Now.Ticks.ToString("x");
        //    string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
        //    try
        //    {
        //        var formdata = string.Format(formdataTemplate, typeName, fileName);
        //        var formdataBytes = Encoding.ASCII.GetBytes(postStream.Length == 0 ? formdata.Substring(2, formdata.Length - 2) : formdata);//第一行不需要换行
        //        postStream.Write(formdataBytes, 0, formdataBytes.Length);
 
        //        //写入文件
        //        byte[] buffer = new byte[1024];
        //        int bytesRead = 0;
        //        while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
        //        {
        //            postStream.Write(buffer, 0, bytesRead);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
 
        //    //结尾
        //    var footer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
        //    postStream.Write(footer, 0, footer.Length);
        //    request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
        //    #endregion
 
        //    request.ContentLength = postStream.Length;
        //    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
        //    request.KeepAlive = true;
        //    request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
 
        //    #region 输入二进制流
        //    if (postStream != null)
        //    {
        //        postStream.Position = 0;
 
        //        //直接写入流
        //        Stream requestStream = request.GetRequestStream();
 
        //        byte[] buffer = new byte[1024];
        //        int bytesRead = 0;
        //        while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0)
        //        {
        //            requestStream.Write(buffer, 0, bytesRead);
        //        }
 
        //        postStream.Close();//关闭文件访问
        //    }
        //    #endregion
 
        //    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        //    using (Stream responseStream = response.GetResponseStream())
        //    {
        //        using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding)))
        //        {
        //            string retString = myStreamReader.ReadToEnd();
        //            return retString;
        //        }
        //    }
        //}
    }
}