开发客户端-服务器 iPhone 应用程序
如果我想开发一个采用客户端-服务器设计的 iPhone 应用程序(iPhone 设备作为客户端和 ac# 服务器),有两个问题:
- 是否可以使用我自己的笔记本电脑来运行服务器?如果不是,我有什么选择?
- 我是否必须开发自己的消息传输协议?
因此,如果我理解正确,从客户端向服务器发送“创建新用户”之类的消息的过程如下: 1. 客户端将创建一个包含命令“CREATE NEW USER”和新用户详细信息的 JSON/XML。 2. 客户端将通过 HTTP 请求(作为 HTTP 请求的正文)发送此 JSON/XML,其中 URL 映射到服务器端的 ac# 方法。 3. 这将触发服务器上适当的方法,并在数据库上创建新用户。 4. 服务器将创建包含重放“CREATED”的 JSON/XML,并将其通过 HTTP 响应发送到客户端(作为 HTTP 响应的正文)。 这是正确的吗?
If i want to develop an iPhone app with a client-server design (iPhone devices as the clients and a c# server),two questions:
- Is it possible to use my own laptop to run the server on? and if not what are my options?
- Do i have to develop my own protocol for transferring messages?
So if i understood right the process of sending a message like "CREATE NEW USER" from the client to the server is as follow:
1. The client will create a JSON/XML containing the command "CREATE NEW USER" and the new user details.
2. The client will send this JSON/XML via HTTP request (as the body of the HTTP request) with a URL that maps to a c# method on the server side.
3. This will trigger the appropriate method on the server and the new user will be created on the database.
4. The server will create JSON/XML containing the replay "CREATED" and will send it to the client vie HTTP response (as the body of the HTTP response).
Is that correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 ASIHTTPRequest 相当轻松地完成此操作。它是一个免费的第三方解决方案,可以与不同类型的 Web 服务交互。
ASIHTTPRequest 网站
You can do this fairly easily with ASIHTTPRequest. It's a free 3rd party solution that can talk to different types of web services.
ASIHTTPRequest website
不确定这是否回答了您的问题,但只是作为一个建议,这样您就不必处理太多服务器端的东西,请使用 解析
Not sure if this answers your question, but just as a suggestion so you don't have to deal a lot with server side stuff, use Parse
您需要通过 http 获取 xml 或 json。 Web 服务和 REST over http 是为了解决您所面临的不同平台之间的互操作性问题而创建的。
由于您使用 C# 作为服务器,因此您可以研究 WCF 并使用 REST 模式或 SOAP(Web 服务)来公开您的操作和数据。关于数据,您可以通过网络将这些对象序列化为 JSON 或 XML。
对于 iPhone 使用,我建议使用 REST(因为它基本上将 url 请求路径映射到 C# 方法)。从手机的角度来看,这只是一个 url 请求,然后返回 xml 或 json 数据。
在 C# 中,您只需创建方法并使用 DataContract 属性装饰它们即可。然后,在您的方法中将它们映射到 url 相对路径。在网络上搜索 WCF 和 REST 服务。您可以在任何主机中从命令行到 Windows 服务再到 IIS 运行它。
http://msdn.microsoft.com/en-us/library/bb412178。 aspx
创建这些 C# 服务时,如果是 REST,您可以在浏览器中发出请求并查看数据。您还应该查看 Fiddler 来检查您的流量:http://www.fiddler2.com/fiddler2/
在手机端,首先需要发出http请求。您可以使用 iOS 类来做到这一点,但像 ASIHTTPRequest 这样的包装器使它变得更容易。一旦收到响应,您就必须对其进行解析。如果您选择 XML,iOS 类会提供解析 xml 响应的简单方法。如果您选择 JSON,那么有像 SBJSON 这样的类。
http://allseeing-i.com/ASIHTTPRequest/ - (阅读此 ASIHTTPRequest 博客 使用前)
https://github.com/stig/json-framework
iPhone 中的 REST Web 服务
还有一个更高级别的框架,称为 RESTKit,它使 iPhone 端更加容易。
https://github.com/RestKit/RestKit
希望能帮助您将其结合在一起。
编辑:
添加创建新用户场景:
推荐/有效的请求负载是什么REST PUT 方法?
REST 中的 PUT 与 POST
http://damianm.com/tech/building-a-rest -client-with-wcf/
You want either xml or json over http. Web Services and REST over http was created for interoperability problems between different platforms which is what you're facing.
Since you're using C# for the server, you can look into WCF and use either a REST pattern or SOAP (web services) to expose your actions and data. Concerning the data you can serialize those objects over the wire as JSON or XML.
For iPhone consumption, I would recommend REST (since that basically maps a url request path to a C# method). From the phones perspective, it's just a url request and xml or json data comes back.
In C# you simply create your methods and decorate them with DataContract attributes. Then, on your methods you map them to url relative paths. Search the net for WCF and REST services. You can run it in any host from a command line to a windows service to IIS.
http://msdn.microsoft.com/en-us/library/bb412178.aspx
When creating those C# services, if REST, you can hit the requests in a browser and see the data come through. You should also look into Fiddler to inspect your traffic: http://www.fiddler2.com/fiddler2/
On the phone side, you first need to make the http request. You can do that with iOS classes but wrappers like ASIHTTPRequest make it much easier. Once you get the response back, you have to parse it. If you choose XML the iOS classes offer simple ways to parse the xml response. If you choose JSON, there's classes like SBJSON.
http://allseeing-i.com/ASIHTTPRequest/ - (Read this ASIHTTPRequest blog before using)
https://github.com/stig/json-framework
rest web services in iphone
There's also a much higher level framework called RESTKit which makes the iPhone side much easier.
https://github.com/RestKit/RestKit
Hope that helps tie it together for you.
EDIT:
Adding the create new user scenario:
What is the recommended/effective request payload for a REST PUT method?
PUT vs POST in REST
http://damianm.com/tech/building-a-rest-client-with-wcf/
我强烈建议依赖 HTTP 协议。不要实现您自己的网络协议!
使用 GET 请求从服务器获取数据,使用 POST 请求将大量数据从客户端发送到服务器。
为了构建数据,请使用 JSON 对数据进行编码。
这是一个很好的教程,解释了如何使用 ASIHTTPRequest 和 JSONKit 执行此操作: http://www.icodeblog.com/2011/09/30/dynamically-controlling-your-application-from-the-web/
是的,您可以运行您的工作机器上的服务器。
I'd strongly recommended to rely on the HTTP protocol. Don't implement your own networking protocol!
Use GET requests to fetch data from the server and POST requests to send big amounts of data from the client to the server.
In order to structure your data, encode the data using JSON.
Here is a nice tutorial that explains how to do that with ASIHTTPRequest and JSONKit: http://www.icodeblog.com/2011/09/30/dynamically-controlling-your-application-from-the-web/
And yes, you can run the server on your working machine.