title: Why would the same exact code crash in one app, and work in the other?
body: <p>I am trying to call a Web API Rest method in an app from a client app. I have two client apps - with one, it works, with the other, it doesn't.</p>
  
  <p>The code to call the REST method is identical. It <em>is</em> the same exact server code, of course.</p>
  
  <p>In the failing client app, it fails on this line:</p>
  
  <pre><code>var webResponse = (HttpWebResponse)webRequest.GetResponse();
  </code></pre>
  
  <p>...and the code is <em>exactly</em> the same in the two apps, up to the point of failure, except that the failing one is in an event handler, and the working one is not. Well, another (related) difference, besides the test code being in an event handler, and the "real" code being in a separate method, is that the separate method is also in a separate class. Why this would make a difference, I don't know, but I'm kind of grasping at straws here. Here it is:</p>
  
  <pre><code>Cursor.Current = Cursors.WaitCursor;
  try
  {
      // Cannot start with String.Empty or " "; they both fail for some reason - Controller method is not even called.
      string lastIDFetched = "0";
      const int RECORDS_TO_FETCH = 100;
      bool moreRecordsExist = true;
  
      try
      {
          while (moreRecordsExist)
          {
              string formatargready_uri = string.Format("http://localhost:28642/api/InventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH);
              var webRequest = (HttpWebRequest)WebRequest.Create(formatargready_uri); 
              // GET is the default method/verb, but it's here for clarity
              webRequest.Method = "GET";
              var webResponse = (HttpWebResponse)webRequest.GetResponse();// &lt;-- this throws an exception when there is no longer any data left
  </code></pre>
  
  <p>Failing method's definition:</p>
  
  <pre><code>private void buttonGetInvItemsInBlocks_Click(object sender, EventArgs e)
  </code></pre>
  
  <p>Working method's definition:</p>
  
  <pre><code>public static List&lt;HHSUtils.InventoryItem&gt; GetBatchOfInventoryItems()
  </code></pre>
  
  <p>On the one that fails, F10 on the last line goes straight to the Exception block (goto considered harmful - no kidding!), but the exception shows me nothing - hovering over this line:</p>
  
  <pre><code>catch (Exception ex)
  </code></pre>
  
  <p>...I see only, "<em>Exception | System.Exception
      base {object} | object</em>"</p>
  
  <p>Both clients are built in Visual Studio 2008 and target .NET 3.5; the server is VS 2013, .NET 4.5.1</p>
  
  <p>Here's a clue, I guess: the failing code has target device set to "Windows CE Device" (it deploys to a handheld device, which I control via "Remote Display Control for Windows CE")</p>
  
  <p>Is anybody in touch with their inner Columbo here?</p>
  
  <h2>UPDATE</h2>
  
  <p>I changed the code from this:</p>
  
  <pre><code>var webResponse = (HttpWebResponse)webRequest.GetResponse();
  </code></pre>
  
  <p>...to this:</p>
  
  <pre><code>using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
  </code></pre>
  
  <p>...and it makes no difference - it still jumps straight from that line to the catch block, with no useful data in the exception displayed.</p>
  
  <p>In my test app, I changed the code to use the "more better" way (using); it works either way, whereas the other app works neither way. IOW: using using is good, but it makes no real difference in this case.</p>
  
  <h2>UPDATE 2</h2>
  
  <p>In response to JayC's answer, I tried all the following "connection strings" - all with the exact same result:</p>
  
  <pre><code>string uri = string.Format("http://localhost:28642/api/InventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); // Fails on "using" line below with no usable exception data
  //string uri = string.Format("http://192.112.263.38:28642/api/InventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); &lt;-- same failure
  //string uri = string.Format("http://192.112.263.38/api/InventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); &lt;-- same failure
  //string uri = string.Format("http://192.112.263.38:80/api/InventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); &lt;-- same failure
  //string uri = string.Format("http://192.112.263.38:777/api/InventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); &lt;-- same failure
  //string uri = string.Format("http://192.112.263.38:28642/api/inventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); &lt;-- same failure
  //string uri = string.Format("http://192.112.263.38/api/inventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); &lt;-- same failure
  //string uri = string.Format("http://192.112.263.38:80/api/inventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); &lt;-- same failure
  //string uri = string.Format("http://192.112.263.38:777/api/inventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); &lt;-- same failure
  </code></pre>
  
  <h2>UPDATE 3</h2>
  
  <p>In response to Roman Gruber's comment about CAS: Wikipedia has a disambiguation page for CAS (<a href="http://en.wikipedia.org/wiki/Cas_(disambiguation)" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Cas_(disambiguation)</a>), but none of the proferred possibilities seem sensible for this context.</p>
  
  <p>The  main article, OTOH, has several CAS acronyms in the Computing section. To which of the following are you referring:</p>
  
  <pre><code>Central Authentication Service, a single sign-on protocol
  Channel Associated Signaling, a type of communication signaling
  Code Access Security in the Microsoft .NET framework
  </code></pre>
  
  <p>?</p>
  
  <h2>UPDATE 4</h2>
  
  <p>Okay, I added the code suggested by a couple of cats:</p>
  
  <pre><code>catch (WebException webex)
  {
      HttpWebResponse hwr = (HttpWebResponse) webex.Response;
      HttpStatusCode hsc = hwr.StatusCode;
      MessageBox.Show(string.Format("{0} Status code == {1}", webex.Message, hsc.ToString()));
  }
  </code></pre>
  
  <p>...and here's what I see when the breakpoint reaches the catch:</p>
  
  <p><img src="https://i.stack.imgur.com/O7OrU.png" alt="enter image description here"></p>
  
  <p>And here's the err msg shown:</p>
  
  <p>"The remote server returned an error: (400) Bad Request. Status code == Bad Request"</p>
  
  <p>I think the problem is in the "connection string"; as can be seen in Update 2, I've tried everything I can think of, and nothing works to connect the device to the desktop.</p>