title: Calling methods from .ashx handler from aspx.cs page
body: <p>I'm having a bit of trouble calling a method from a generic handler we have. I've tried using two separate techniques to call a simple 'HelloWorld()' method but I get two different errors:</p>
  
  <p>The first technique is as follows:</p>
  
  <pre><code>        WebClient wc = new WebClient();
  
          NameValueCollection formData = new NameValueCollection();
          formData["method"] = "HelloWorld";
  
  
          byte[] data;
  
          try
          {
              data = wc.UploadValues(_domain, formData);
          }
          catch (WebException ex)
          {
              Label1.Text = ex.Message;
              return;
          }
  
          string response = Encoding.UTF8.GetString(data);
          Label1.Text = response;
  
          wc.Dispose();
  </code></pre>
  
  <p>and I get the following error:</p>
  
  <pre><code>{"id":null,"error":{"name":"Found String where Object was expected."}}
  </code></pre>
  
  <p>and the second technique I've tried is:</p>
  
  <pre><code>            var httpWebRequest = (HttpWebRequest)WebRequest.Create(_domain);
          httpWebRequest.ContentType = "text/json";
          httpWebRequest.Method = "POST";
  
          using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
          {
              string json = "{\"method\":\"helloWorld\"}"; //," +
              //"\"password\":\"bla\"}";
  
              streamWriter.Write(json);
              streamWriter.Flush();
              streamWriter.Close();
  
              try
              {
                  var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                  using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                  {
                      var result = streamReader.ReadToEnd();
                  }
              }
              catch (WebException wex)
              {
                  Label2.Text = wex.Message;
              }
              catch (Exception ex)
              {
                  Label2.Text = ex.Message;
              }
          }
  </code></pre>
  
  <p>and with this, I get the following error:</p>
  
  <pre><code>The remote server returned an error: (500) Internal Server Error.
  </code></pre>
  
  <p>When I test the call from the ".ashx?test" page the method runs and the details at the bottom of the screen are:</p>
  
  <pre><code>Pragma: no-cache
  Date: Tue, 23 Jul 2013 13:46:19 GMT
  Server: ASP.NET Development Server/11.0.0.0
  X-AspNet-Version: 2.0.50727
  Content-Type: application/json; charset=utf-8
  Cache-Control: no-cache
  Connection: Close
  Content-Length: 32
  Expires: -1
  </code></pre>
  
  <p>Any ideas as to why this wouldn't be working?</p>
  
  <p>Thanks!</p>