Permalink
Browse files

Runtime fixes for C# Snippet.

Changes to make the c# snippet more accessible to a wider variety of
skill level and certain issues that would occur at runtime. Added
Content-Type to header loop to prevent improper attempt to add to Header
Collection. Fixes attempt to write the content length to the request
during the write process. Fixes issue where the bytes may not be written
to underlying stream. Adds a common SSL certificate hook that can be
uncommented by developer.
  • Loading branch information...
1 parent 8fd30d1 commit bd13087fd45bc9e767964e41edecfc58e3fb47a9 @arkbuilder arkbuilder committed Dec 11, 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 bd13087

Please sign in to comment.