我想使用 HTTP 连接到服务器,向它发送一个字符串并从服务器接收响应字符串。关于如何通过 url 连接到服务器、发送和接收消息的任何想法?
iphony
问问题
1575 次
3 回答
4
// create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData that will hold
// the received data
// receivedData is declared as a method instance elsewhere
receivedData=[[NSMutableData data] retain];
} else {
// inform the user that the download could not be made
}
于 2009-02-26T10:21:09.513 回答
4
我建议使用来自 All-Seeing Interactive 的优秀 ASIHTTPRequest 源:http: //allseeing-i.com/ASIHTTPRequest。我正在这样做,还有几个已发布的 iPhone 应用程序也是如此,因此您可以确定代码非常可靠。
这是 CFNetwork API 的包装器,它使与 Web 服务器通信的一些更繁琐的方面变得更容易。它是用 Objective-C 编写的,适用于 Mac OS X 和 iPhone 应用程序。
它适用于执行基本的 HTTP 请求并与基于 REST 的服务(GET / POST / PUT / DELETE)进行交互。ASIFormDataRequest 子类使使用 multipart/form-data 提交 POST 数据和文件变得容易。
于 2009-02-26T13:53:19.363 回答
1
NSMutableURLRequest
您可以使用和类连接到服务器、发送字符串并接收响应NSURLConnection
。一个很好的起点是URL 加载系统简介。
于 2009-02-26T10:17:45.840 回答