Permalink
Browse files

Merge pull request #9 from arkbuilder/master

Runtime fixes for C# Snippet.
  • Loading branch information...
2 parents 8fd30d1 + bd13087 commit d61dbd998cef17edd88d513ea4bac93494b64a18 @Almad Almad committed Dec 12, 2013
Showing with 14 additions and 9 deletions.
  1. +14 −9 csharp.html
View
@@ -1,14 +1,21 @@
<section name="csharp" class="csharp">
<p class="ioDesc">Request</p>
- <pre class="incoming"><code class="language-csharp">var request = System.Net.WebRequest.Create("<%= @apiUrl %><%= @url %>") as System.Net.HttpWebRequest;
- request.Method = "<%= @method.toUpperCase() %>";
+ <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 %>";
@@ -26,17 +33,15 @@
<% end %>
<% if @body?.length > 0: %>
<% if @method.toUpperCase() is 'POST' or @method.toUpperCase() is 'PUT' or @method.toUpperCase() is 'DELETE' or @body.length > 0: %>
-using (var writer = new System.IO.StreamWriter(request.GetRequestStream())) {
- byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(<%= @helpers.escape @body %>);
- request.ContentLength = byteArray.Length;
- writer.Write(byteArray);
- writer.Close();
- }
+
+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;
+string responseContent=null;
using (var response = request.GetResponse() as System.Net.HttpWebResponse) {
using (var reader = new System.IO.StreamReader(response.GetResponseStream())) {
responseContent = reader.ReadToEnd();

0 comments on commit d61dbd9

Please sign in to comment.