0

正如我在这里被提醒的那样,我可能需要使用“ppp_peer”以编程方式从我的 Compact Framework 应用程序连接到我的 PC 上运行的 Web API 应用程序。

我试过这个(用“ppp_peer”替换IPAddress):

string uri = string.Format("http://ppp_peer:28642/api/FileTransfer/GetHHSetupUpdate?serialNum={0}&clientVersion={1}", serNum, clientVer);

...但我在“Main”中得到“ NullReferenceException ”(在此之前我得到“无法连接到远程服务器”)。

我在服务器代码中有一个断点,但它没有到达那个断点,所以它必须在客户端中发生这种情况的某个地方。

上下文中的客户端代码是:

string uri = string.Format("http://ppp_peer:28642/api/FileTransfer/GetHHSetupUpdate?serialNum={0}&clientVersion={1}", serNum, clientVer);
RESTfulMethods.DownloadNewerVersionOfHHSetup(uri);
. . .
public static void DownloadNewerVersionOfHHSetup(string uri)
{
    string dateElements = DateTime.Now.ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture);
    var outputFileName = string.Format("HHSetup_{0}.exe", dateElements);
    try
    {
        var webRequest = (HttpWebRequest)WebRequest.Create(uri);
        var webResponse = (HttpWebResponse)webRequest.GetResponse();
        string statusCode = webResponse.StatusCode.ToString();
        if (statusCode == "NoContent")
        {
            MessageBox.Show("You already have the newest available version.");
        }
        else
        {
            var responseStream = webResponse.GetResponseStream();
            using (Stream file = File.Create(outputFileName))
            {
                CopyStream(responseStream, file);
                MessageBox.Show(string.Format("New version downloaded to {0}", outputFileName));
            }
        }
    }
    catch (WebException webex)
    {
        string msg = webex.Message;
        string innerEx = webex.InnerException.ToString();
        string status = webex.Status.ToString();
        MessageBox.Show(string.Format("Message: {0}; Status: {1}; inner Ex: {2}", msg, status, innerEx));
    }
}

除了ppp_peer 之外,我还需要一个 IPAddress ,还是我的 URI 格式错误,或者...???

更新

在“NRE”之后我还看到,“”......遇到严重错误,必须关闭“

我从上面更改了代码,以查看 ppp_peer 被翻译为:

IPAddress ipAd = Dns.Resolve("PPP_PEER").AddressList[0];
string IPAddr = ipAd.ToString();
MessageBox.Show(IPAddr);
string uri = string.Format("http://{0}:28642/api/FileTransfer/GetHHSetupUpdate?serialNum={1}&clientVersion={2}", IPAddr, serNum, clientVer);

MessageBox 调用显示“192.168.55.100”,这与我认为我的 PC 的 IPAddress 不同......???

我也一样:

IPAddress ipAd = Dns.GetHostEntry("PPP_PEER").AddressList[0];

更新 2

改用它(我从这里得到它[通过 ActiveSync 连接时从 Windows Mobile 获取主机 pc 的 IP 地址):

IPAddress ipAd = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];

...显示的 IP 地址是“一个向上”(192.168.55.101),而不是 NRE,我得到:

消息:无法连接到远程服务器;状态:连接失败;内部示例:System.Net.Sockets.SocketException:无法建立连接,因为目标机器在 System.Net.Sockets.SocketConnectNoCheck(EndPoint remoteEP) ...

所以看来我在客户端尽我所能,服务器听到敲门声,但没有开门——对吗?

顺便说一句,出于好奇,我还添加了以下代码:

string hostName = Dns.GetHostName();
MessageBox.Show(string.Format("host name is {0}", hostName));

...我看到“ WindowsCE

更新 3

根据Andy Wiggly(编写“MS .NET Compact Framework”的猫/家伙)的这篇文章,您确实使用了“ppp_peer”:

HttpWebRequest request = REST.CreateRequest(@"http://ppp_peer/DataServicesWebsite/NorthwindService.svc/Customers",
              HttpMethods.GET, String.Empty, @"application/atom+xml", "", "");

最有趣的是缺少端口分配(“:28642”或其他);然而,这种风格也给了我一个 NRE(是的,有点像 Null Ready to Eat)。

更新 4

那么从手持设备访问主机需要什么uri呢?

我已经尝试了来自客户端/紧凑框架应用程序的所有以下排列,但没有一个工作:

IPAddress ipAd = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
string IPAddr = ipAd.ToString();

//string uri = string.Format("http://ppp_peer/api/...
//string uri = string.Format("http://ppp_peer:28642/api...
//string uri = string.Format("http://PPP_PEER/api/...
string uri = string.Format("http://PPP_PEER:28642/api/...
//string uri = string.Format("http://{0}:28642/api/...
//string uri = string.Format("http://192.168.125.50:28642/api/...
//string uri = string.Format("http://Platypus:28642/api/...
RESTfulMethods.DownloadNewerVersionOfHHSetup(uri);

该错误发生在该客户端代码中的某处(无法单步执行,所以我不知道确切的位置),因为我在显示的最后一行有一个断点,并且永远无法到达。

服务器(Web API)代码:

[Route("api/FileTransfer/GetUpdatedHHSetup")]
public HttpResponseMessage GetUpdate(string serialNum, string clientVersion)
{
    return _fileTransfer.GetHHSetupUpdate(serialNum, clientVersion);
}

public HttpResponseMessage GetHHSetupUpdate(string serialNum, string clientVersion)
{
    HttpResponseMessage result;
    string filePath = GetAvailableUpdateForCustomer(serialNum); // <= breakpoint on this  
        line

我在 DownloadNewerVersionOfHHSetup() 中添加了一些调试行,现在它看起来像这样:

public static void DownloadNewerVersionOfHHSetup(string uri)
{
    MessageBox.Show("Made it into DownloadNewerVersionOfHHSetup");
    string dateElements = DateTime.Now.ToString("yyyyMMddHHmmssfff", 
CultureInfo.InvariantCulture);
    var outputFileName = string.Format("HHSetup_{0}.exe", dateElements);
    try
    {
        var webRequest = (HttpWebRequest)WebRequest.Create(uri);
        MessageBox.Show("Made it into DownloadNewerVersionOfHHSetup #2");
        var webResponse = (HttpWebResponse)webRequest.GetResponse();
        MessageBox.Show("Made it into DownloadNewerVersionOfHHSetup #3");
        . . .

我从来没有看到“#3”,所以它一定是调用 GetResponse() 内部的一个问题,但我怎样才能找出到底是什么?我得到 NRE,然后“ ...遇到严重错误,必须关闭

这是它尝试调用服务器的地方,但如前所述,它永远不会到达被调用的服务器方法......

更新 5

事实证明,这现在有效:

http://192.168.125.50:28642/api/

...它这样做的主要原因是因为我的路由属性(GetUpdatedHHSetup)与我从客户端调用的属性(GetHHSetupUpdate)不匹配。一旦我对齐了这些行星,NRE 就消失了,我得到了预期的结果。

4

1 回答 1

0

URI/连接字符串中不需要 PPP_PEER。现在可以使用主机 PC 的 IP 地址(以及服务器/Web API 应用程序的端口号)(在修复了路由属性与客户端调用它的内容之间的不匹配之后)。

我认为使用机器名称也可以。

于 2014-03-11T22:58:28.703 回答