title: Why it works using Ajax POST but not using HttpWebRequest?
body: <p>Here's a strange thing: I'm trying to send data to a Coldfusion web service via HttpWebRequest (also tried with HttpClient), and I always get a "login page" as response.</p>
  
  <p>BUT, if I do the same thing as Ajax Post, it works.</p>
  
  <p>BUT², if I put the content-type as "application/json" in the Ajax call, it returns the login page as well.</p>
  
  <p>The web service manager says that the service doesn't need login, since we're using VPN to call it. But if I try to access the webservice URI via browser, it opens the login page.</p>
  
  <p>The code in C#:
  <strong>[EDIT] Created the object using JsonConvert</strong></p>
  
  <pre><code>var request = HttpWebRequest.Create("http://url.cfc");
  
  var obj= new 
                                      {
                                          method = "MethodName",
                                          data1 = "123456",
                                          data2 = "aaa"
                                      };
  
  string postData = JsonConvert.SerializeObject(obj);
  
  request.Method = "POST";
  //request.ContentType = "application/json"; (not using!!!)
  
  using (var writer = new StreamWriter(request .GetRequestStream()))
          {
              writer.Write(postData );
              writer.Flush();
              writer.Close();
          }
  
  var response = (HttpWebResponse)request.GetResponse();
  
  var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
  </code></pre>
  
  <p>And the code in JS:</p>
  
  <pre><code>$.ajax({
        type: "post",
        url: "http://url.cfc",
        data: ({
            method: "MethodName",
          data1: 123456,
          data2: "aaa" }),
         dataType: "json",
        success: function (result) { 
    console.log(result); },
        error: function (result) { 
    console.log(result); },
     });
  });
  </code></pre>
  
  <p>Is there some substantial difference between Ajax call and HttpWebRequest call that can "block" the request using C#? Or maybe I'm failing in put some important data in HttpWebRequest's Header? Moreover: some issue in Coldfusion's web service authorization configuration?</p>