从手持设备与PC进行通讯时,我需要哪种uri模式? [英] What uri pattern do I need to communicate with my PC from my handheld device?
问题描述
提醒我此处,我可能需要使用"ppp_peer"以编程方式从Compact Framework应用程序连接到PC上运行的Web API应用程序.
As I was reminded here, I need to probably use "ppp_peer" to programmatically connect from my Compact Framework app to my Web API app running on my PC.
我已经尝试过(用"ppp_peer"替换IPAddress):
I have tried this (replacing an IPAddress with "ppp_peer"):
string uri = string.Format("http://ppp_peer:28642/api/FileTransfer/GetHHSetupUpdate?serialNum={0}&clientVersion={1}", serNum, clientVer);
...但是我在"Main"中得到了" NullReferenceException "(在此之前,我得到了无法连接到远程服务器").
...but I get, "NullReferenceException" in "Main" (prior to this I got "Unable to Connect to the Remote Server").
我在服务器代码中有一个断点,但没有达到断点,因此它必须在客户端中发生此情况的某个地方.
I have a breakpoint in the server code, but it doesn't reach that, so it must be somewhere in the client where this is occurring.
上下文中的客户端代码为:
The client code in context is:
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之外,我还需要IP地址还是我的URI格式错误,或者... ???
Do I need an IPAddress in addition to ppp_peer, or is my formatting of the URI wrong, or...???
在"NRE"之后,我还看到" ...遇到了严重的错误,必须关闭".
After the "NRE" I also see, ""...encountered a serious error and must shut down"
我从上面更改了代码,以查看ppp_peer的翻译为:
I changed the code from above to see just what ppp_peer is translated as:
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的IP地址不同... ???
The MessageBox call shows me "192.168.55.100" which is different from what I thought my PC's IPAddress was...???
我的理解与之相同:
IPAddress ipAd = Dns.GetHostEntry("PPP_PEER").AddressList[0];
更新2
改用它(我是从这里[ ...显示的IP地址是一个"(192.168.55.101),而不是NRE,我得到:
...the IP Address displayed is "one up" (192.168.55.101), and instead of an NRE, I get:
消息:无法连接到远程服务器.状态:ConnectFailure;内部示例:System.Net.Sockets.SocketException:无法建立连接,因为目标计算机在System.Net.Sockets.SocketConnectNoCheck(EndPoint remoteEP)上主动拒绝了它.
所以看来我正在尽我所能在客户端上,服务器听到了敲门声,但没有打开门-是吗?
So it seems I'm doing all I can on the client end, and the server hears the knock, but is not opening the door - am I right?
顺便说一句,出于好奇,我还添加了以下代码:
BTW, out of curiosity I also added this code:
string hostName = Dns.GetHostName();
MessageBox.Show(string.Format("host name is {0}", hostName));
...然后我看到" WindowsCE "
...and I see "WindowsCE"
According to this post by Andy Wiggly (the cat/bloke who wrote "MS .NET Compact Framework"), you do use "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).
The interestingest thing about this is the lack of a port assignment (":28642" or whatever); however, this style also gives me an NRE (yes, kind of like a Null Ready to Eat).
那么从手持设备访问主机需要多少uri?
So what uri will it take to access the host machine from the handheld device?
我已经尝试从客户端/Compact Framework应用程序进行以下所有排列,但均无用:
I have tried all of the following permutations from the client/Compact Framework app, and none work:
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);
该错误发生在该客户端代码的某个地方(无法逐步执行,因此我不知道确切的位置),因为我在所示的最后一行上有一个断点,并且从未达到过.
The error is happening somewhere in that client code (can't step through it, so I don't know exactly where), because I have a breakpoint on the last line shown, and it is never reached.
服务器(Web API)代码:
SERVER (Web API) code:
[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()中放置了一些调试行,以便现在看起来像这样:
I put some debug lines in DownloadNewerVersionOfHHSetup() so that it now looks like this:
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,然后" ...遇到了严重错误,必须关闭"
I never see "#3", so it must be a problem inside the call to GetResponse(), but how can I find out exactly what? I get the NRE, then "...encountered a serious error and must shut down"
这是它尝试调用服务器的地方,但是如上所述,它从未进入正在调用的服务器方法...
This is where it tries to call the server but, as mentioned, it never makes it to the server method being called...
事实证明这现在可行:
http://192.168.125.50:28642/api/
...并且这样做的主要原因是因为我的路由属性(GetUpdatedHHSetup)与我从客户端调用的内容(GetHHSetupUpdate)之间不匹配.一旦我对准了这些行星,NRE就消失了,我得到了预期的结果.
...and the main reason that it does is because there was a mismatch between my routing attribute (GetUpdatedHHSetup) and what I was calling from the client (GetHHSetupUpdate). Once I aligned those planets, the NRE went away, and I got the expected result.
推荐答案
PPP_PEER.现在可以使用主机PC的IP地址(以及服务器/Web API应用程序的端口号)(在解决路由属性与客户端调用它之间的不匹配之后).
PPP_PEER is not needed in the uri/connection string. Using the host PC's IP Address (and port number for the server/Web API app) works now (after fixing the mismatch between the routing attribute and what the client was calling it).
我认为使用机器名也可以.
I reckon using the machine name would work just as well, too.
这篇关于从手持设备与PC进行通讯时,我需要哪种uri模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!