forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebRequestExtensions.cs
More file actions
222 lines (181 loc) · 7.82 KB
/
WebRequestExtensions.cs
File metadata and controls
222 lines (181 loc) · 7.82 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#if !SILVERLIGHT
using System;
using System.IO;
using System.Net;
using System.Text;
using ServiceStack.Common;
using ServiceStack.Common.Web;
using ServiceStack.Text;
namespace ServiceStack.ServiceClient.Web
{
public static class WebRequestExtensions
{
public static string DownloadJsonFromUrl(this string url)
{
return url.DownloadUrl(ContentType.Json);
}
public static string DownloadXmlFromUrl(this string url)
{
return url.DownloadUrl(ContentType.Xml);
}
public static string DownloadCsvFromUrl(this string url)
{
return url.DownloadUrl(ContentType.Csv);
}
public static string DownloadUrl(this string url, string acceptContentType)
{
var webReq = (HttpWebRequest)WebRequest.Create(url);
webReq.Accept = acceptContentType;
using (var webRes = webReq.GetResponse())
return DownloadText(webRes);
}
public static string DownloadUrl(this string url)
{
var webReq = WebRequest.Create(url);
using (var webRes = webReq.GetResponse())
return DownloadText(webRes);
}
public static byte[] DownloadBinaryFromUrl(this string url)
{
var webReq = WebRequest.Create(url);
using (var webRes = webReq.GetResponse())
return DownloadBinary(webRes);
}
public static string PostJsonToUrl(this string url, string data)
{
return SendToUrl(url, HttpMethod.Post, Encoding.UTF8.GetBytes(data), ContentType.Json, ContentType.Json);
}
public static string PostJsonToUrl(this string url, object data)
{
return SendToUrl(url, HttpMethod.Post, Encoding.UTF8.GetBytes(data.ToJson()), ContentType.Json, ContentType.Json);
}
public static string PostToUrl(this string url, string data, string requestContentType = null, string acceptContentType = null)
{
return SendToUrl(url, HttpMethod.Post, Encoding.UTF8.GetBytes(data), requestContentType, acceptContentType);
}
public static string PutToUrl(this string url, string data, string requestContentType = null, string acceptContentType = null)
{
return SendToUrl(url, HttpMethod.Put, Encoding.UTF8.GetBytes(data), requestContentType, acceptContentType);
}
public static string PostToUrl(this string url, byte[] data, string requestContentType = null, string acceptContentType = null)
{
return SendToUrl(url, HttpMethod.Post, data, requestContentType, acceptContentType);
}
public static string PutToUrl(this string url, byte[] data, string requestContentType = null, string acceptContentType = null)
{
return SendToUrl(url, HttpMethod.Put, data, requestContentType, acceptContentType);
}
private static string SendToUrl(string url, string httpMethod, byte[] data, string requestContentType = null, string acceptContentType = null)
{
var webReq = (HttpWebRequest) WebRequest.Create(url);
webReq.Method = httpMethod;
if (requestContentType != null)
webReq.ContentType = requestContentType;
if (acceptContentType != null)
webReq.Accept = acceptContentType;
try
{
using (var req = webReq.GetRequestStream())
req.Write(data, 0, data.Length);
}
catch (Exception ex)
{
Console.WriteLine("Error sending Request: " + ex);
throw;
}
try
{
using (var webRes = webReq.GetResponse())
return DownloadText(webRes);
}
catch (Exception ex)
{
Console.WriteLine("Error reading Response: " + ex);
throw;
}
}
public static string DownloadAsString(this string url)
{
var webReq = WebRequest.Create(url);
using (var webRes = webReq.GetResponse())
return DownloadText(webRes);
}
public static string DownloadText(this WebResponse webRes)
{
using (var stream = webRes.GetResponseStream())
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
public static byte[] DownloadBinary(this WebResponse webRes)
{
using (var stream = webRes.GetResponseStream())
{
return stream.ReadFully();
}
}
public static HttpWebResponse GetErrorResponse(this string url)
{
try
{
var webReq = WebRequest.Create(url);
var webRes = webReq.GetResponse();
var strRes = webRes.DownloadText();
Console.WriteLine("Expected error, got: " + strRes);
return null;
}
catch (WebException webEx)
{
return (HttpWebResponse)webEx.Response;
}
}
public static WebResponse UploadFile(this WebRequest webRequest,
FileInfo uploadFileInfo, string uploadFileMimeType)
{
using (var fileStream = uploadFileInfo.OpenRead())
{
var fileName = uploadFileInfo.Name;
webRequest.UploadFile(fileStream, fileName, uploadFileMimeType);
}
return webRequest.GetResponse();
}
public static void UploadFile(this WebRequest webRequest, Stream fileStream, string fileName, string mimeType)
{
var httpReq = (HttpWebRequest)webRequest;
httpReq.UserAgent = Env.ServerUserAgent;
httpReq.Method = "POST";
httpReq.AllowAutoRedirect = false;
httpReq.KeepAlive = false;
var boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
httpReq.ContentType = "multipart/form-data; boundary=" + boundary;
var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
var headerTemplate = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\nContent-Type: {1}\r\n\r\n";
var header = string.Format(headerTemplate, fileName, mimeType);
var headerbytes = System.Text.Encoding.ASCII.GetBytes(header);
httpReq.ContentLength = fileStream.Length + headerbytes.Length + boundarybytes.Length;
using (Stream outputStream = httpReq.GetRequestStream())
{
outputStream.Write(headerbytes, 0, headerbytes.Length);
byte[] buffer = new byte[4096];
int byteCount;
while ((byteCount = fileStream.Read(buffer, 0, 4096)) > 0)
{
outputStream.Write(buffer, 0, byteCount);
}
outputStream.Write(boundarybytes, 0, boundarybytes.Length);
outputStream.Close();
}
}
public static void UploadFile(this WebRequest webRequest, Stream fileStream, string fileName)
{
fileName.ThrowIfNull("fileName");
var mimeType = MimeTypes.GetMimeType(fileName);
if (mimeType == null)
throw new ArgumentException("Mime-type not found for file: " + fileName);
UploadFile(webRequest, fileStream, fileName, mimeType);
}
}
}
#endif