![]()
using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Net;
6 using System.Net.Http;
7 using System.Net.Http.Headers;
8 using System.Text;
9 using System.Threading.Tasks;
10
11 namespace GZAPI.Common
12 {
13 public class httpHelper
14 {
15 public static string Post(
string url,
string data)
16 {
17 string returnData =
null;
18 try
19 {
20 //byte[] buffer = Encoding.UTF8.GetBytes(data);
21 //HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
22 //webReq.Method = "POST";
23 //webReq.ContentType = "application/x-www-form-urlencoded";
24 //webReq.ContentLength = buffer.Length;
25 //Stream postData = webReq.GetRequestStream();
26 //webReq.Timeout = 99999999;
27 ////webReq.ReadWriteTimeout = 99999999;
28 //postData.Write(buffer, 0, buffer.Length);
29 //postData.Close();
30 //HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
31 //Stream answer = webResp.GetResponseStream();
32 //StreamReader answerData = new StreamReader(answer);
33 //returnData = answerData.ReadToEnd();
34
35 string paraUrlCoded = data;
36 byte[] payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
37
38 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
39 request.Method = "
POST";
40 request.ContentType = "
application/json;charset=UTF-8";
41 request.ContentLength = payload.Length;
42
43 using (Stream writer = request.GetRequestStream())
44 {
45 writer.Write(payload, 0, payload.Length);
46
47 using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse())
48 {
49
50 using (System.IO.Stream s = response.GetResponseStream())
51 {
52 string StrDate = "
";
53 string strValue = "
";
54 using (StreamReader Reader =
new StreamReader(s, Encoding.UTF8))
55 {
56 while ((StrDate = Reader.ReadLine()) !=
null)
57 {
58 strValue += StrDate + Environment.NewLine;
59 }
60 returnData = strValue;
61 }
62 }
63 }
64 }
65 }
66 catch
67 {
68 return "
";
69 }
70 return returnData.Trim() + "
";
71 }
72
73
74 public async
static void SysnPost(
string url,
string json)
75 {
76 // Clear text of Output textbox
77
78 using (HttpClient httpClient =
new HttpClient())
79 {
80
81
82 try
83 {
84
85 httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("
application/json"));
86 using (var content =
new StringContent(json, Encoding.UTF8, "
application/json"))
87 {
88
89 HttpResponseMessage wcfResponse = await httpClient.PostAsync(url, content);
90
91
92
93 //string str = wcfResponse.Content.ReadAsStringAsync();
94 //await DisplayTextResult(wcfResponse, OutputField);
95 }
96 }
97 catch (HttpRequestException hre)
98 {
99 //NotifyUser("Error:" + hre.Message);
100 }
101 catch (TaskCanceledException)
102 {
103 //NotifyUser("Request canceled.");
104 }
105 catch (Exception ex)
106 {
107 //NotifyUser(ex.Message);
108 }
109 finally
110 {
111
112 }
113 }
114
115 }
116
117 public static string Get(
string URL)
118 {
119 String ReCode =
string.Empty;
120 try
121 {
122 HttpWebRequest wNetr = (HttpWebRequest)HttpWebRequest.Create(URL);
123 HttpWebResponse wNetp = (HttpWebResponse)wNetr.GetResponse();
124 wNetr.ContentType = "
text/html";
125 wNetr.Method = "
Get";
126 Stream Streams = wNetp.GetResponseStream();
127 StreamReader Reads =
new StreamReader(Streams, Encoding.UTF8);
128 ReCode = Reads.ReadToEnd();
129
130 //封闭临时不实用的资料
131 Reads.Dispose();
132 Streams.Dispose();
133 wNetp.Close();
134 }
135 catch (Exception ex) {
return ex.Message; }
136
137 return ReCode;
138
139 }
140 }
141 }
142