title: POST to REST API from winform returns empty
body: <p>I have a project that uses the <a href="https://www4.hockeystreams.com/api#login" rel="nofollow">hockeystreams.com API</a>.  I am having trouble with using their API to login from my WinForm.  It always gives me a blank response, whether I give a bad username/password combination or a correct one.  My code is as follows:</p>
  
  <pre><code>public static bool LogIn(string username, string password)
      {
          string URL = string.Format("{0}&amp;username={1}&amp;password={2}", Global.LOGIN_API, username, password);
          HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
          webRequest.ContentType = "application/json; charset=utf-8";
          webRequest.Accept = "application/json, text/javascript, */*";
          webRequest.Method = "POST";
          try
          {
  
              WebResponse response = webRequest.GetResponse();
              Stream stream = response.GetResponseStream();
              string json = "";
  
              using (StreamReader reader = new StreamReader(stream))
              {
                  while (!reader.EndOfStream)
                  {
                      json += reader.ReadLine();
                  }
              }
  
              LogInJSON deserializedLogInData = JsonConvert.DeserializeObject&lt;LogInJSON&gt;(json);
              if (deserializedLogInData.Status == "Success")
              {
                  Global.TOKEN = deserializedLogInData.Token;
                  Global.USERNAME = deserializedLogInData.Username;
                  Global.ISPREMIUM = deserializedLogInData.Membership == "REGULAR" ? false : true;
                  Global.FAVORITETEAM = deserializedLogInData.Favteam;
  
                  return true;
              }
              else
              {
                  return false;
              }
          }
  </code></pre>
  
  <p>I have tried changing the POST to PUT, but that throws a 406 error (Bad Request).  I am very green with JSON so I don't know where to go from here.  Any help greatly appreciated!</p>
  
  <p>EDIT:
  I found this on the hockeystreams website that is similar to my issue but the are using apache httpclient.</p>
  
  <pre><code>    With buckinpgh's help I resolved my issue.
  
  Just for anyone else out there who is wondering, the solution (if your using apache HttpClient) is to create a HttpPost request with the url: "https://api.hockeystreams.com/Login?"
  
  then add two headers:
  1) StringEntity with the value: "username=&lt;username&gt;&amp;password=&lt;password&gt;&amp;key=&lt;apiKey&gt;"
  2) "content-type", "application/x-www-form-urlencoded"
  
  after that you can just call HttpClient.execute with your request. Of course you'll have to parse the json result, but at least now you have a result.
  </code></pre>
  
  <p>I changed my code to try and follow it as best I could with the HttpWebRequest:</p>
  
  <pre><code>public static bool LogIn(string username, string password)
      {
          string URL = Global.LOGIN_PATH;
          HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
          webRequest.ContentType = "application/x-www-form-urlencoded";
          webRequest.Accept = "application/json, text/javascript, */*";
          webRequest.Method = "POST";
          webRequest.Headers.Add("StringEntity", string.Format("username={0}&amp;password={1}&amp;key={2}", username, password, Global.APIKEY);
          try 
          {
              WebResponse response = webRequest.GetResponse();
              Stream stream = response.GetResponseStream();
              string json = "";
  </code></pre>
  
  <p>But it still comes back with a blank response.  Is there a different way around this I'm not seeing?</p>
  
  <p>EDIT 2:</p>
  
  <p>Ended up with this code fixing it.   I used RestSharp for this.</p>
  
  <pre><code>public static bool LogIn(string username, string password)
          {
              string URL = Global.LOGIN_PATH;
              try
              {
                  var client = new RestClient("https://api.hockeystreams.com/");
                  var request = new RestRequest("login?", Method.POST);
                  request.AddHeader("content-type", "application/x-www-forms-urlencoded");
                  request.AddParameter("username", username);
                  request.AddParameter("password", password);
                  request.AddParameter("key", Global.APIKEY);
                  IRestResponse response = client.Execute(request);
                  var json = response.Content;
  
                  LogInJSON deserializedLogInData = JsonConvert.DeserializeObject&lt;LogInJSON&gt;(json);
  
                  try
                  {
                      if (deserializedLogInData.Status == "Success")
                      {
                          Global.TOKEN = deserializedLogInData.Token;
                          Global.USERNAME = deserializedLogInData.Username;
                          Global.ISPREMIUM = deserializedLogInData.Membership == "REGULAR" ? false : true;
                          Global.FAVORITETEAM = deserializedLogInData.Favteam;
  
                          return true;
                      }
                      else
                      {
                          Global.ERROR = deserializedLogInData.Msg;
                          return false;
                      }
                  }
  </code></pre>