Difference between POST requests and GET requests
1. the URL created in the post request method does not contain parameters
2. Create a request using the NSMutableRequest class
First, you must note that a free interface is required to implement this request. The URL in my website is a free interface, but the number of free calls is limited, I hope you will forgive me a lot. To create a single view project, the above controls are simple. I will not implement them using code one by one, so I will drag them directly, as shown in:
Vc7EtbXA78Pmv7S/release + zbLusru24NXGztXBy6GjwO/D5rT6wuvT0NK7sr + release + vH87e9t6gKvt/release + upload "brush: java;"> # import @ Class HHLViewController; @ interface HHLAppDelegate: UIResponder @ Property (strong, nonatomic) UIWindow * window; @ property (strong, nonatomic) HHLViewController * viewController; @ end
HHLAppDelegate. m
#import "HHLAppDelegate.h"#import "HHLViewController.h"@implementation HHLAppDelegate- (void)dealloc{ [_window release]; [_viewController release]; [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES;}- (void)applicationWillResignActive:(UIApplication *)application{ // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application{ // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application{ // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application{ // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application{ // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end
HHLViewController. h
#import
@interface HHLViewController : UIViewController
{ NSMutableString *_resultStr;}@property (retain, nonatomic) IBOutlet UITextField *textFieldQQ;@property (retain, nonatomic) IBOutlet UILabel *labelResult;- (IBAction)btnSearch:(id)sender;@end
HHLViewController. m
# Import "HHLViewController. h "@ interface HHLViewController () @ end @ implementation HHLViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} -(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} -(void) dealloc {[_ textFieldQQ release]; [_ labelResult release]; [super d Ealloc];} // Method for associating the start query button-(IBAction) btnSearch :( id) sender {/* GET request // obtain the QQ number NSString * pStr = self in the current textField. textFieldQQ. text; // concatenate it into a string // NSString * strUrl = [@ "http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline? QqCode = "stringByAppendingString: pStr]; NSString * strURL = [@" http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline? QqCode = "stringByAppendingFormat: @" % @ ", pStr]; // convert to url nsurl * pURL = [NSURL URLWithString: strURL]; // create a request NSURLRequest * pRequest = [NSURLRequest requestWithURL: pURL cachePolicy: Required timeoutInterval: 60]; // initiate a request to the server [NSURLConnection connectionWithRequest: pRequest delegate: self]; * // * POST Request * // The first difference between the request and the Get request (without parameters, the parameter attachment is in the body) NSString * postStr = @ "http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline "; // convert to url nsurl * postURL = [NSURL URLWithString: postStr]; // The second difference (request is required) NSMutableURLRequest * postRequest = [NSMutableURLRequest requestWithURL: postURL cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60]; // make the parameter into a string NSString * postStr1 = [NSString stringWithFormat: @ "qqCode = % @", self. textFieldQQ. text]; // convert to NSData * postData = [postStr1 dataUsingEncoding: NSUTF8StringEncoding]; // The third difference (using the parameter as the Body) [postRequest setHTTPBody: postData]; // point 4 (the current request method must be manually declared as POST) [postRequest setHTTPMethod: @ "POST"]; // send the request to the server [NSURLConnection connectionWithRequest: postRequest delegate: self] ;}# pragma mark URLConnectionDataDelegate-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {NSLog (@ "server response "); _ resultStr = [[NSMutableString alloc] init];}-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {NSLog (@ "Accept data "); // convert data to the string NSString * pStr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] Through UTF-8 encoding; // put it in the string of the storage result [_ resultStr appendString: pStr]; NSLog (@ "pStr = % @", _ resultStr);}-(void) connectionDidFinishLoading :( NSURLConnection *) connection {NSLog (@ "accepted"); NSString * pTempStr = [_ resultStr substringWithRange: NSMakeRange (78, 1)]; NSLog (@ "pTempStr = % @", pTempStr); // call the output result method // self. labelResult. text = pTempStr; [self result: pTempStr];}-(void) result :( NSString *) str {if (NSOrderedSame = [str compare: @ "Y"]) {NSLog (@ "online"); self. labelResult. text = @ "your query QQ online";} else if (NSOrderedSame = [str compare: @ "V"]) {NSLog (@ "the number of free queries is exceeded "); self. labelResult. text = @ "the number of free queries has been exceeded" ;}}-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (@ "error: % @", [error localizedDescription]);} // Let the keyboard bounce back-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {[self. view endEditing: YES];} @ end
Enter the corresponding QQ number after executing the program. The effect is as follows: