我正在尝试从客户端应用程序调用应用程序中的 Web API Rest 方法。我有两个客户端应用程序 - 一个可以工作,另一个不能。
调用 REST 方法的代码是相同的。当然,它是完全相同的服务器代码。
在失败的客户端应用程序中,它在这一行失败:
var webResponse = (HttpWebResponse)webRequest.GetResponse();
...并且两个应用程序中的代码完全相同,直到出现故障点,除了失败的应用程序在事件处理程序中,而工作的应用程序不在。好吧,除了测试代码在事件处理程序中和“真实”代码在单独的方法中之外,另一个(相关的)区别是,单独的方法也在单独的类中。为什么这会有所作为,我不知道,但我在这里有点抓住稻草。这里是:
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();// <-- this throws an exception when there is no longer any data left
失败方法的定义:
private void buttonGetInvItemsInBlocks_Click(object sender, EventArgs e)
工作方法定义:
public static List<HHSUtils.InventoryItem> GetBatchOfInventoryItems()
在失败的那个上,最后一行的 F10 直接进入 Exception 块(goto 被认为是有害的 - 不开玩笑!),但异常什么也没显示 - 悬停在这条线上:
catch (Exception ex)
...我只看到,“ Exception | System.Exception base {object} | object ”
两个客户端均内置于 Visual Studio 2008 并针对 .NET 3.5;服务器是 VS 2013,.NET 4.5.1
我猜这是一条线索:失败的代码将目标设备设置为“Windows CE 设备”(它部署到手持设备,我通过“Windows CE 的远程显示控制”进行控制)
有人在这里与他们的内部哥伦布保持联系吗?
更新
我从这里更改了代码:
var webResponse = (HttpWebResponse)webRequest.GetResponse();
...对此:
using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
...并且没有区别 - 它仍然从该行直接跳转到 catch 块,在显示的异常中没有有用的数据。
在我的测试应用程序中,我更改了代码以使用“更好”的方式(使用);它以任何一种方式工作,而另一个应用程序则不能。IOW:使用 using 很好,但在这种情况下并没有真正的区别。
更新 2
为了回应 JayC 的回答,我尝试了以下所有“连接字符串”——结果完全相同:
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); <-- same failure
//string uri = string.Format("http://192.112.263.38/api/InventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); <-- same failure
//string uri = string.Format("http://192.112.263.38:80/api/InventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); <-- same failure
//string uri = string.Format("http://192.112.263.38:777/api/InventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); <-- same failure
//string uri = string.Format("http://192.112.263.38:28642/api/inventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); <-- same failure
//string uri = string.Format("http://192.112.263.38/api/inventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); <-- same failure
//string uri = string.Format("http://192.112.263.38:80/api/inventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); <-- same failure
//string uri = string.Format("http://192.112.263.38:777/api/inventoryItems/{0}/{1}", lastIDFetched, RECORDS_TO_FETCH); <-- same failure
更新 3
作为对 Roman Gruber 对 CAS 的评论的回应:维基百科有一个 CAS 的消歧页面(http://en.wikipedia.org/wiki/Cas_(disambiguation)),但在这种情况下,所提供的可能性似乎都不合理。
主要文章 OTOH 在计算部分有几个 CAS 首字母缩略词。您指的是以下哪一项:
Central Authentication Service, a single sign-on protocol
Channel Associated Signaling, a type of communication signaling
Code Access Security in the Microsoft .NET framework
?
更新 4
好的,我添加了几只猫建议的代码:
catch (WebException webex)
{
HttpWebResponse hwr = (HttpWebResponse) webex.Response;
HttpStatusCode hsc = hwr.StatusCode;
MessageBox.Show(string.Format("{0} Status code == {1}", webex.Message, hsc.ToString()));
}
...这就是我在断点到达捕获点时看到的内容:
这是显示的错误消息:
“远程服务器返回错误:(400) 错误请求。状态码 == 错误请求”
我认为问题出在“连接字符串”上;从更新 2 中可以看出,我已经尝试了所有我能想到的方法,但无法将设备连接到桌面。