我刚刚回答了一个类似的问题,所以我将在此处复制并粘贴一些代码:
- (void)myFunctionToSendDataToTheServer
{
MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"myServer.com"];
MKNetworkOperation *op = [engine operationWithPath:@"/service.asmx"];
op.postDataEncoding = MKNKPostDataEncodingTypeCustom;
[op setCustomPostDataEncodingHandler:^NSString *(NSDictionary *postDataDict) {
return [NSString stringWithFormat:@"<R><Root><Grade>33</grade></Root></R>"];
} forType:@"application/xml"];
[engine enqueueOperation:op];
}
我建议添加第三方库来处理网络代码。在这种情况下,我使用了 MKNetworkKit。
显示“[engine enqueueOperation:op];”的代码行 是将请求发送到服务器的部分。如果您改变主意并且确实想要或需要处理响应,请在“[engine enqueueOperation:op];”之前添加类似这样的内容:
[op onCompletion:^(MKNetworkOperation *completedOperation) {
NSLog(@"Successful response from server: %@", [completedOperation responseString]);
} onError:^(NSError *error) {
NSLog(@"Error from server; %@", [error debugDescription]);
}];