post请求

#import"AppDelegate.h"

@implementationAppDelegate

//post请求

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

{

self.window= [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];

//创建URL

NSString*urlStr =@"http://localhost:8080/HelloWorld/NewServlet";

NSURL*url =[NSURLURLWithString:urlStr];

//创建请求(post请求使用NSMutableURLRequest因为我们要修改请求报文)

NSMutableURLRequest*request =[NSMutableURLRequestrequestWithURL:url];

//设置HTTP请求方式

[request setHTTPMethod:@"POST"];

NSString*bodyStr =@"zhanghao=lisi&mima=123";

//字符串转化成data

NSData*data =[bodyStr  dataUsingEncoding:NSUTF8StringEncoding];

//设置HTTP请求体

[request  setHTTPBody:data];

NSData*adata=[requestHTTPBody];

NSString*stra=[[NSString  alloc]initWithData:adataencoding:NSUTF8StringEncoding];

NSLog(@"----------))))----%@",stra);

NSHTTPURLResponse*response =nil;

//

NSData*data1 =[NSURLConnection     sendSynchronousRequest:requestreturningResponse:&responseerror:nil];

NSLog(@"---->>>>%d",[response   statusCode]);

NSString*dataStr =[[NSString    alloc]initWithData:data1encoding:NSUTF8StringEncoding];

UIAlertView*alert =[[UIAlertView   alloc]initWithTitle:@"哈哈哈"message:dataStrdelegate:nil  cancelButtonTitle:nil  otherButtonTitles:@"确定",nil];

[alertshow];

[alertrelease];

// Override point for customization after application launch.

self.window.backgroundColor= [UIColor  whiteColor];

[self.windowmakeKeyAndVisible];

returnYES;

}


post请求_第1张图片
post请求_第2张图片

你可能感兴趣的:(post请求)