我的服务器上运行了一个运行我的Web服务的PHP代码。它以发送整数值处理数据。我怎么能得到它?这是我的请求网址:
NSString *requestURL=[NSString stringWithFormat:@"%@?u=%@& p=%@&platform=ios",url,txtUserName.text,txtPassword.text];
更新评论:我的服务器上有一个php文件。它需要3个参数并注册我的用户并返回值为1(成功,2表示重复)。我需要将请求发送到我的服务器:
url="http://smwebtech.com/Pandit/web_service/signup.php?u=Test&p=password&platform=ios"
如何将此请求发送到服务器并从服务器获取返回值?
答案 0 :(得分:6)
您可以使用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 :(得分:0)
如果您正在寻找一种向服务器发出HTTP请求的方法,那么有许多框架可以帮助您实现这一目标。直到最近,ASIHTTPRequest是我所青睐的,但不幸的是它已经停止。
Google's API Client Library也是一个很好的选择,以及ASIHTTPRequest创建者的number of others suggested。
应该有很多文档可以帮助你入门。
编辑:要使用ASIHTTPRequest
发出特定请求,请在具有协议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, i.e.
// convert it to a JSON object using the json-framework (https://github.com/stig/json-framework/)
}
答案 2 :(得分:0)
下面是您可以用来发送请求和获得响应的方法。
-(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”。 我希望这会有所帮助。