title: POST or Get Request with Json response in C# windows Form App
body: <p>I made an API in server side with PHP + Laravel framework which accept both GET &amp; Post requests with some special parameters.</p>
  
  <p>It's address is : <a href="http://beresun.ir/API/Orders/0" rel="nofollow noreferrer">http://beresun.ir/API/Orders/0</a></p>
  
  <p>and it gets these parameters :</p>
  
  <pre><code>token &gt; string ,
  restaurant_id &gt; integer ,
  admin_id &gt; integer ,
  token_id &gt; integer .
  </code></pre>
  
  <p>if we send a request with GET method with these parameters, for example it will be :</p>
  
  <p><a href="http://beresun.ir/API/Orders/0?token=2JEuksuv86DcFmLrQa7nna4QDeowuGTqpyUK0pf9wSlbe6D5hLtEVxvzMT5gAZG0xBKy00HxS3J79mcr8F54dBD0uIg5HX5fzPOAP&amp;restaurant_id=1&amp;admin_id=2&amp;token_id=40" rel="nofollow noreferrer">http://beresun.ir/API/Orders/0?token=2JEuksuv86DcFmLrQa7nna4QDeowuGTqpyUK0pf9wSlbe6D5hLtEVxvzMT5gAZG0xBKy00HxS3J79mcr8F54dBD0uIg5HX5fzPOAP&amp;restaurant_id=1&amp;admin_id=2&amp;token_id=40</a> which returns a json data , you can click on the link to see the results .</p>
  
  <p>the response json data includes some information about customers and it's products.</p>
  
  <p>now I want to make a windows application for this service with C# and request data from this API with POST or GET methods :</p>
  
  <p>I want to use this API to get Json data from Web server and save them in my Windows application , So I created two functions in one of my Form Classes :</p>
  
  <pre><code>using Newtonsoft.Json;
  using System;
  using System.IO;
  using System.Net;
  using System.Text;
  using System.Windows.Forms;
  
  public partial class MainActivity : Form
  {
      string token = "2JEuksuv86DcFmLrQa7nna4QDeowuGTqpyUK0pf9wSlbe6D5hLtEVxvzMT5gAZG0xBKy00HxS3J79mcr8F54dBD0uIg5HX5fzPOAP";
      int restaurant_id = 1;
      int admin_id = 2;
      int token_id = 40;
  private void SendWebrequest_Get_Method()
  {
      try
      {
  
          String postData = "token=" + token +
                  "&amp;restaurant_id=" + restaurant_id +
                  "&amp;admin_id=" + admin_id +
                  "&amp;token_id=" + token_id;
  
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://beresun.ir/API/Orders/0?" + postData);
          request.ContentType = "application/json; charset=utf-8";
          request.Accept = "application/json";
          request.Method = WebRequestMethods.Http.Get;
  
          WebResponse response = request.GetResponse();
  
          StreamReader sr = new StreamReader(response.GetResponseStream());
  
          String json_text = sr.ReadToEnd();
          dynamic stuff = JsonConvert.DeserializeObject(json_text);
          if (stuff.error != null)
          {
              MessageBox.Show("problem with getting data", "Error");
  
          }
          else
          {
              MessageBox.Show(json_text, "success");
          }
  
          sr.Close();
      }
  
      catch (Exception ex)
  
      {
          MessageBox.Show("Wrong request ! " + ex.Message, "Error");
      }
  }
  
  
  private void SendWebrequest_POST_Method()
  {
      try
      {
          // Create a request using a URL that can receive a post. 
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://beresun.ir/API/Orders/5");
          // Set the Method property of the request to POST.
          request.Method = "POST";
          request.Credentials = CredentialCache.DefaultCredentials;
          ((HttpWebRequest)request).UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
          // Create POST data and convert it to a byte array.
          string postData = "token=" + token +
              "&amp;restaurant_id=" + restaurant_id +
              "&amp;admin_id=" + admin_id +
              "&amp;token_id=" + token_id;
          byte[] byteArray = Encoding.UTF8.GetBytes(postData);
          // Set the ContentType property of the WebRequest.
          request.ContentType = "application/json; charset=utf-8";
          // Set the ContentLength property of the WebRequest.
          request.ContentLength = byteArray.Length;
          // Get the request stream.
          Stream dataStream = request.GetRequestStream();
          // Write the data to the request stream.
          dataStream.Write(byteArray, 0, byteArray.Length);
          // Close the Stream object.
          dataStream.Close();
          // Get the response.
          WebResponse response = request.GetResponse();
          // Display the status.
          MessageBox.Show(((HttpWebResponse)response).StatusDescription);
          // Get the stream containing content returned by the server.
          dataStream = response.GetResponseStream();
          // Open the stream using a StreamReader for easy access.
          StreamReader reader = new StreamReader(dataStream);
          // Read the content.
          string responseFromServer = reader.ReadToEnd();
          // Display the content.
          MessageBox.Show(responseFromServer);
          // Clean up the streams.
          reader.Close();
          dataStream.Close();
          response.Close();
      }
  
      catch (Exception ex)
      {
          MessageBox.Show("Wrong request ! " + ex.Message, "Error");
      }
  }
  }
  </code></pre>
  
  <p>Now Here is the problem , when I test the API it works fine , but when I request data from my application , it returns error and not working . </p>
  
  <p>Can anyone explain me how I should request data from this API , to get data , I searched a lot , and I used many different methods , but none of them worked for me . maybe because this API returns very much Json data or maybe request timeout happen. I don't know , I couldn't find the problem . So I asked it Here. </p>
  
  <p>I don't know what I should do . </p>
  
  <p>Thanks</p>