forked from apiaryio/language-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsharp.html
49 lines (46 loc) · 2.77 KB
/
csharp.html
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
<section name="csharp" class="csharp">
<p class="ioDesc">Request</p>
<pre class="incoming"><code class="language-csharp">
//Common testing requirement. If you are consuming an API in a sandbox/test region, uncomment this line of code ONLY for non production uses.
//System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var request = System.Net.WebRequest.Create("<%= @apiUrl %><%= @url %>") as System.Net.HttpWebRequest;
request.KeepAlive = true;
request.Method = "<%= @method.toUpperCase() %>";
<% if @contentType: %>
request.ContentType = "<%= @contentType %>";
<% end %>
<% if @headers and @helpers.isNotEmpty @headers: %>
<% for header, value of @headers: %>
<% header = header.toLowerCase() %>
<% if header is 'accept': %>request.Accept = "<%= @helpers.escape value %>";
<% else if header is 'content-type': %>request.ContentType=<%= @helpers.escape value %>;
<% else if header is 'connection': %>request.Connection = "<%= @helpers.escape value %>";
<% else if header is 'content-length': %>request.ContentLength = "<%= @helpers.escape value %>";
<% else if header is 'expect': %>request.Expect = "<%= @helpers.escape value %>";
<% else if header is 'date': %>request.Date = "<%= @helpers.escape value %>";
<% else if header is 'host': %>request.Host = "<%= @helpers.escape value %>";
<% else if header is 'if-modified-since': %>request.IfModifiedSince = "<%= @helpers.escape value %>";
<% else if header is 'range': %>request.AddRange = "<%= @helpers.escape value %>";
<% else if header is 'referer': %>request.Referer = "<%= @helpers.escape value %>";
<% else if header is 'transfer-encoding': %>request.TransferEncoding = "<%= @helpers.escape value %>";
<% else if header is 'user-agent': %>request.UserAgent = "<%= @helpers.escape value %>";
<% else if header is 'connection': %>request.Connection = "<%= @helpers.escape value %>";
<% else: %>request.Headers.Add(<%= @helpers.escape header %>, <%= @helpers.escape value %>);
<% end %>
<% end %>
<% end %>
<% if @body?.length > 0: %>
<% if @method.toUpperCase() is 'POST' or @method.toUpperCase() is 'PUT' or @method.toUpperCase() is 'DELETE' or @body.length > 0: %>
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(<%= @helpers.escape @body %>);
request.ContentLength = byteArray.Length;
using (var writer = request.GetRequestStream()){writer.Write(byteArray, 0, byteArray.Length);}
<% end %>
<% else: %>
request.ContentLength = 0;
<% end %>
string responseContent=null;
using (var response = request.GetResponse() as System.Net.HttpWebResponse) {
using (var reader = new System.IO.StreamReader(response.GetResponseStream())) {
responseContent = reader.ReadToEnd();
}
}</code></pre></section>