title: How can I avoid a "Bad Request - Invalid Hostname" error when making a REST call from a Compact Framework client?
body: <p>I used this code from <a href="http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.80).aspx" rel="noreferrer">here</a> to try to call a REST Controller method on a Web API server app from a Compact Framework client:</p>
  
  <pre><code>public static void SendXMLFile3(string uri, string data)
  {
      WebRequest request = WebRequest.Create (uri);
      request.Method = "POST";
      string postData = data;
      byte[] byteArray = Encoding.UTF8.GetBytes (postData);
      request.ContentType = "application/x-www-form-urlencoded";
      request.ContentLength = byteArray.Length;
      Stream dataStream = request.GetRequestStream ();
      dataStream.Write (byteArray, 0, byteArray.Length);
      dataStream.Close ();
      WebResponse response = request.GetResponse ();
      MessageBox.Show(((HttpWebResponse) response).StatusDescription);
      dataStream = response.GetResponseStream ();
      StreamReader reader = new StreamReader (dataStream);
      string responseFromServer = reader.ReadToEnd();
      MessageBox.Show(responseFromServer);
      reader.Close ();
      dataStream.Close ();
      response.Close ();
  }
  </code></pre>
  
  <p>...I had earlier tried this code, which I got from the book "Microsoft .NET Compact Framework":</p>
  
  <pre><code>public static void SendXMLFile2(string uri, string data)
  {
      WebRequest req = WebRequest.Create(uri);
      req.Method = "Post";
      req.ContentType = "text/plain; charset=utf-8";
      byte[] encodedBytes = Encoding.UTF8.GetBytes(data);
      req.ContentLength = encodedBytes.Length;
  
      Stream requestStream = req.GetRequestStream();
      requestStream.Write(encodedBytes, 0, encodedBytes.Length);
      requestStream.Close();
  
      WebResponse result = req.GetResponse();
      MessageBox.Show(result.ToString());
  }
  </code></pre>
  
  <p>...but I get "400 - Bad Request" with the new (as well as the old) code.</p>
  
  <p>My initial attempt also does not work, with the same result (400):</p>
  
  <pre><code>public static string SendXMLFile(string xmlFilepath, string uri, int timeout)
  {
      HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(uri);
      myHttpWebRequest.AllowWriteStreamBuffering=false;
      string postData = "&lt;Command&gt;&lt;DSD&gt;&lt;line_id&gt;1&lt;/line_id&gt;&lt;invoice_no&gt;david_dsd&lt;/invoice_no&gt;. . .&lt;/DSD&gt;&lt;/Command&gt;"; // TODO: if this works, replace it with the real data
      myHttpWebRequest.Method="POST";
      UTF8Encoding encodedData = new UTF8Encoding();
      byte[]  byteArray=encodedData.GetBytes(postData);
      myHttpWebRequest.ContentType = "application/xml";
      myHttpWebRequest.ContentLength=byteArray.Length;
      Stream newStream=myHttpWebRequest.GetRequestStream();
      newStream.Write(byteArray,0,byteArray.Length);
      newStream.Close();
      HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
      return myHttpWebResponse.StatusDescription;
  }
  </code></pre>
  
  <p>There is much more about the plethora of variations I have tried <a href="https://stackoverflow.com/questions/25652692/what-is-wrong-with-this-httpwebrequest-httpwebresponse-code-why-is-it-considere">here</a>, where I have reached my length-of-post limit.</p>
  
  <h2>UPDATE</h2>
  
  <p>Note that the Server code doesn't know/care that the file is XML:</p>
  
  <pre><code>[HttpPost]
  [Route("api/inventory/sendXML/{userId}/{pwd}/{filename}")]
  public async Task SendInventoryXML(String userId, String pwd, String fileName)
  {
      Task task = Request.Content.ReadAsStreamAsync().ContinueWith(t =&gt;
      {
          var stream = t.Result;
          using (FileStream fileStream = File.Create(String.Format(@"C:\HDP\{0}.xml", fileName), (int)stream.Length))
          {
              byte[] bytesInStream = new byte[stream.Length];
              stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
              fileStream.Write(bytesInStream, 0, bytesInStream.Length);
          }
      });
  }
  </code></pre>
  
  <h2>UPDATE 2</h2>
  
  <p>I tried Charles to see if it would pick up the local HTTP traffic, but it is also deaf to such (like Fiddler, without special ministrations, anyway). This is what Charles looks like after getting the "400 - Bad Request" error:</p>
  
  <p><img src="https://i.stack.imgur.com/MIM6H.png" alt="enter image description here"></p>
  
  <h2>UPDATE 3</h2>
  
  <p>I found this suggestion somewhere to get Fiddler to show local HTTP traffic:</p>
  
  <pre><code>Tools--&gt; Fiddler Options. Choose Connections tab. Check the 'USe PAC Script' option.
  </code></pre>
  
  <p>...but it didn't work - I still see no HTTP traffic when getting the "400 (Bad Request)" message.</p>
  
  <h2>UPDATE 4</h2>
  
  <p>I am now seeing "400 (Bad Request)" in Fiddler 2, too; to get it, I enter any of the following in Postman (don't see this in Fiddler when calling from CE/CF/handheld app):</p>
  
  <pre><code>http://SHANNON2:21608/api/inventory/sendXML/su/su/blablee // Hostname
  http://SHANNON2.:21608/api/inventory/sendXML/su/su/blablee // Hostname with Fiddler-fooler appended "."
  http://192.168.125.50:21608/api/inventory/sendXML/su/su/blablee // IP Address
  </code></pre>
  
  <p>(Fiddler does not capture anything if I replace the hostname or IP Address with "localhost")</p>
  
  <p>Note: For these URLs in Postman, I have "Post" (as opposed to GET, etc.) selected, and an XML file attached.</p>
  
  <p>Inspectors.Headers in Fiddler shows: </p>
  
  <pre><code>POST /api/inventory/sendXML/su/su/blablee HTTP/1.1
  </code></pre>
  
  <p>Although I consider this a minor debugging victory, I still don't see why I'm getting the "400" error.</p>
  
  <p>Fiddler tells me, in the Inspectors.WebView pane:</p>
  
  <pre><code>Bad Request - Invalid Hostname
  
  --------------------------------------------------------------------------------
  
  HTTP Error 400. The request hostname is invalid.
  </code></pre>
  
  <p>How can that be? When I run it from Postman, I hit the breakpoint in the server - if the hostname is invalid, why is it being reached?</p>
  
  <h2>UPDATE 5</h2>
  
  <p>Testing the call from Fiddler Composer and Postman, the only way I can reach the breakpoint in the server code is by using "localhost" - replacing that with the PC's IPAddress (192.168.125.50) or HostName (SHANNON2) does not reach the breakpoint. While "interesting," calling "localhost" from the handheld device is obviously not an option.</p>
  
  <h2>UPDATE 6</h2>
  
  <p>Related new question <a href="https://stackoverflow.com/questions/25878787/how-can-i-solve-http-404-and-405-error-msgs/25879083#25879083">here</a>.</p>
  
  <h2>UPDATE 7</h2>
  
  <p>The crux of the biscuit was adding at the command prompt either this:</p>
  
  <pre><code>netsh http add urlacl url=http://shannon2:80/ user=everyone
  </code></pre>
  
  <p>...or this:</p>
  
  <pre><code>netsh http add urlacl url=http://shannon2:8080/ user=everyone
  </code></pre>
  
  <p>See Update 5 <a href="https://stackoverflow.com/questions/25878787/how-can-i-solve-http-404-and-405-error-msgs">here</a> for more details</p>