发送请求并获取响应

我有一个运行在我的服务器上的php代码,我打电话给我的web服务。它处理数据发送整数value.How我可以得到吗?这是我的请求url:

NSString *requestURL=[NSString stringWithFormat:@"%@?u=%@& p=%@&platform=ios",url,txtUserName.text,txtPassword.text]; 

更新评论:我有一个php文件在我的server.It需要3个参数,并注册我的用户,并返回值1(成功,2重复)。我需要发送请求到我的服务器为:

 url="http://smwebtech.com/Pandit/web_service/signup.php?u=Test&p=password&platform=ios" 

我怎样才能发送这个请求到服务器,并从服务器获取返回值?

你可以使用NSURLConnection。 您应该使用实现NSURLConnectionDataDelegate协议并使用NSURLConnection类。

 -(void) requestPage { NSString *urlString = @"http://the.page.you.want.com"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0f]; responseData = [[NSMutableData alloc] init]; connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain]; delegate = target; } -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if ([response isKindOfClass:[NSHTTPURLResponse class]]) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; //If you need the response, you can use it here } } -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responseData appendData:data]; } -(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [responseData release]; [connection release]; } -(void) connectionDidFinishLoading:(NSURLConnection *)connection { if (connection == adCheckConnection) { NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; //You've got all the data now //Do something with your response string [responseString release]; } [responseData release]; [connection release]; } 
  1. 请访问HTTPRequest并根据给定的指导将其设置在您的项目中。
  2. TouchJSON – 下载并放到你的项目中。
  3. 下面是你可以用来发送请求和获取响应的方法。

      -(void) SendAsyncReq:(NSString *)urlString { NSLog(@"URL IS: %@",urlString); NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url]; [request1 setDelegate:self]; [request1 startAsynchronous]; } /// THIS METHOD WILL BE CALLED IF REQUEST IS COMPLETED SUCCESSFULLY - (void)requestFinished:(ASIHTTPRequest *)response { NSData *responseData = [response responseData]; CJSONDeserializer *jsonDeserializer = [CJSONDeserializer deserializer]; NSDictionary *resultsDictionary = [jsonDeserializer deserializeAsDictionary:responseData error:nil]; NSLog(@"Response is: %@",resultsDictionary); } /// THIS METHOD WILL BE CALLED IF REQUEST IS FAILED DUE TO SOME REASON. - (void)requestFailed:(ASIHTTPRequest *)response { NSError *error = [response error]; NSLog(@"%d", [error code]); if([error code] !=4) { NSString *errorMessage = [error localizedDescription]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; [alertView release]; } } 

你必须在你的类中导入#import“CJSONDeserializer.h”和#import“ASIHTTPRequest.h”。 我希望这将有所帮助。

如果您正在寻找一种向服务器发送HTTP请求的方法,那么有许多框架可以帮助您做到这一点。 直到最近, ASIHTTPRequest是我所喜欢的,但不幸的是它已经停产。

Google的API客户端库也是一个很好的select,以及ASIHTTPRequest的创build者build议的其他许多其他的。

那里应该有大量的文档让你开始。

编辑:为了使您的具体请求与ASIHTTPRequest ,使用协议ASIHTTPRequestDelegate类中执行以下ASIHTTPRequestDelegate

 /** * Make a request to the server. */ - (void) makeRequest { // compile your URL string and make the request NSString *requestURL = [NSString stringWithFormat:@"%@?u=%@& p=%@&platform=ios", url, txtUserName.text, txtPassword.text]; NSURL *url = [NSURL URLWithString:requestURL]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request startAsynchronous]; } /** * Handle the response from the previous request. * @see ASIHTTPRequestDelegate */ - (void) requestFinished:(ASIHTTPRequest*)request { NSString *responseString = [request responseString]; // do whatever you need with the response, ie // convert it to a JSON object using the json-framework (https://github.com/stig/json-framework/) }