开发客户端-服务器 iPhone 应用程序

发布于 2024-12-07 15:07:02 字数 435 浏览 1 评论 0原文

如果我想开发一个采用客户端-服务器设计的 iPhone 应用程序(iPhone 设备作为客户端和 ac# 服务器),有两个问题:

  1. 是否可以使用我自己的笔记本电脑来运行服务器?如果不是,我有什么选择?
  2. 我是否必须开发自己的消息传输协议?

因此,如果我理解正确,从客户端向服务器发送“创建新用户”之类的消息的过程如下: 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:

  1. Is it possible to use my own laptop to run the server on? and if not what are my options?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

转身以后 2024-12-14 15:07:03

您可以使用 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

睫毛溺水了 2024-12-14 15:07:03

不确定这是否回答了您的问题,但只是作为一个建议,这样您就不必处理太多服务器端的东西,请使用 解析

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

往事风中埋 2024-12-14 15:07:02

您需要通过 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

希望能帮助您将其结合在一起。

编辑:
添加创建新用户场景:

客户端使用数据(用户名、密码等)创建一个用户对象,并将 HTTP PUT 请求发送到 http://yourserver/myservice/users。客户端将用户对象序列化为正文中的 JSON/XML。

推荐/有效的请求负载是什么REST PUT 方法?

REST 中的 PUT 与 POST

服务器接收到请求。在服务器上,您有一个 WCF“myservice”服务(它是一个类)。它有一个“public User CreateUser(User user)”方法。在该方法中,它通过执行任何必须执行的操作(调用数据库等...)来创建用户对象。它返回 User 对象,因为服务器可能添加了用户 ID 等信息。下面的文章有一个 put 请求示例。

http://damianm.com/tech/building-a-rest -client-with-wcf/

客户端将获得响应,并且包含 id 等所有详细信息的用户对象将以 JSON/XML 形式出现在正文中。它将反序列化为手机上的 User 对象。

服务器还可能暴露以下内容:
/用户/{id} -->公共用户 GetUser(字符串 ID);

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:

The client creates a user object with the data (username, password etc...) and sends an HTTP PUT request to http://yourserver/myservice/users. The client serializes the user object to JSON/XML in the body.

What is the recommended/effective request payload for a REST PUT method?

PUT vs POST in REST

The server receives the request. On the server, you have a WCF "myservice" service (it's a class). It has a "public User CreateUser(User user)" method. In that method it creates a user object by doing whatever it has to do (call database etc...). It returns the User object because perhaps the server added info like the user id. The article below has a put request example.

http://damianm.com/tech/building-a-rest-client-with-wcf/

The client would get the response and the user object with all the details like id, etc... would be in the body as JSON/XML. It would deserialize that into a User object on the phone.

The server could also expose things like:
/User/{id} --> public User GetUser(string id);

江南月 2024-12-14 15:07:02

我强烈建议依赖 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文