Search code examples
.netgetwebrequest

sending GET request and getting cannot send a content body with verb type


I am trying a send a GET request as shown below but I keep on getting, the following error on my console:

Test_API_Access.Exception:Cannot send a content-body with this verb-type.
System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
   at System.Net.HttpWebRequest.CheckProtocol(Boolean onRequestStream)
   at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   at System.Net.HttpWebRequest.GetRequestStream()
   at Test_API_Access.Program.Test_API_Access() in c:\Users\<username>\Documents\Visual Studio 2013\Projects\Test_API_Access\Test_API_Access\Program.cs:line 43

I read another related thread on Stack overflow which is mentioned here: Cannot send a content-body with this verb-type

But I think, I have been using using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse()) in my try block which could have avoided this error?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.Runtime.Serialization;
using System.IO;
namespace Test_API_Access
{ class Program
   {  static void Main(string[] args)
      { Test_API_Access();}
      private static void Test_API_Access()
       {  var publicKey = "publickeygoeshere";
          var privateKey = "privatekeygoeshere";
          var MyDateTime = DateTime.UtcNow.ToString();
          var stringToSign = "POST" + "\n\n\n" + MyDateTime + "\n\n\n";
          var signature = HmacSha1SignRequest(privateKey, stringToSign.ToString());
          var resTreq = new StringBuilder();
          var Url = "https://exvvii.com/webservice/testapi/123456";
          try
           {  // make web service call
              byte[] dataStream = Encoding.UTF8.GetBytes(resTreq.ToString());
              var webRequest = WebRequest.Create(Url);
              webRequest.Method = "GET";
              webRequest.ContentType = "application/x-www-form-urlencoded";
              webRequest.ContentLength = dataStream.Length;
              webRequest.Headers["datetime"] = MyDateTime;
              webRequest.Headers["Authorization"] = publicKey + ":" + signature;
              var newStream = webRequest.GetRequestStream();  // THIS IS LINE #43
              newStream.Write(dataStream, 0, dataStream.Length);
              long length = 0;
              try
              {  using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
                    {   length = response.ContentLength;
                        var header = response.GetResponseStream();
                        Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                        StreamReader readStream = new StreamReader(header, encode);
                        Char[] read = new Char[256];
                        // Reads 256 characters at a time.
                        int count = readStream.Read(read, 0, 256);
                        while (count > 0)
                        { // Dumps the 256 characters on a string and displays the string to the console.
                        String str = new String(read, 0, count);
                        Console.Write(str);
                        count = readStream.Read(read, 0, 256);
                        }
                        Console.WriteLine("");
                        // Releases the resources of the response.
                        response.Close();
                        // Releases the resources of the Stream.
                        readStream.Close();
                    }
                }
                catch (WebException ex)
                {
                    // Log exception and throw as for GET example above
                }
                Console.ReadLine();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Test_API_Access.Exception:" + ex.Message);
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }

        }
        public static string HmacSha1SignRequest(string privateKey, string valueToHash)
        {   System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(privateKey);
            HMACSHA1 hmacsha1 = new HMACSHA1(keyByte);
            byte[] messageBytes = encoding.GetBytes(valueToHash);
            byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
            var hashedValue = Convert.ToBase64String(hashmessage);
            return hashedValue;
        }
    }
}

Solution

  • I've found the solution to this in case anyone comes across this error while searching, basically to get the request stream you need to be using http POST:

    webRequest.Method = "GET";
    

    needs to be

    webRequest.Method = "POST";
    

    I found the answer here: http://forums.asp.net/t/1706210.aspx?Cannot+send+a+content+body+with+this+verb+type