我尝试连接到web服务并从中检索数据,但我没有成功。这是我所做的。连接到objective-c中的REST风格的web服务
这是我试图连接到http://www.rcsb.org/pdb/software/rest.do
这里是我的example.h文件文件中的Web服务:
#import <Foundation/Foundation.h>
@interface example : NSObject {
NSMutableData *receivedData;
}
@property(nonatomic, retain) NSMutableData *receivedData;
- (void) getDataFromServer;
@end
这里是我的example.m文件:
进口“ example.h“
@implementation example
@synthesize receivedData;
-(void) getDataFromServer {
//prepare request
NSString *urlString = [NSString stringWithFormat:@"http://www.rcsb.org/pdb/rest/search/"];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
[theRequest setURL:[NSURL URLWithString:urlString]];
[theRequest setHTTPMethod:@"POST"];
NSString *myXmlQuery = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><orgPdbQuery><version>head</version><queryType>org.pdb.query.simple.AdvancedKeywordQuery</queryType><description>Text Search for: chloro</description><keywords>chloro</keywords></orgPdbQuery>"];
//set Headers
NSString *contentType = [NSString stringWithFormat:@"application/xml"];
[theRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:[NSString stringWithFormat:@"%ld", [myXmlQuery length]] forHTTPHeaderField:@"Content-Length"];
//create the Body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[myXmlQuery dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
//post
[theRequest setHTTPBody:postBody];
NSLog(@"%@", myXmlQuery);
//get response
NSHTTPURLResponse *urlResponse = [[NSHTTPURLResponse alloc] init];
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response code- %ld",[urlResponse statusCode]);
NSLog(@"Response: %@", result);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"in didReceiveResponse....setting receivedData to zero");
//[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"In didReceiveData...receiving data and appending to receivedData");
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection failed with error");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"in connectionDidFinishLoading");
NSLog(@"Succeeded! Received %ld bytes of data", [receivedData length]);
}
@end
我收到一个html页面作为对此的回应。而且,connectionDidFinishLoading没有被调用,因为我没有在输出中获取NSLog语句。我所得到的是我试图连接的url的html版本。
任何形式的帮助将不胜感激。谢谢。
PS:我试图正确格式化这个文本,但它会自动得到这样的格式。抱歉给你带来不便。
它很小,但REST URL是'/ pdb/rest/search'。你有附加的斜线。一些网络服务对这些事情有点挑剔。 –