- //同步post
- -(NSString *)postSyn:(NSString *)urlStr jsonData:(NSString *)jsonData{
- NSLog(@"post_begin");
- NSData* postData = [jsonData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];//數據轉碼;
- NSString *length = [NSString stringWithFormat:@"%d", [postData length]];
- NSMutableURLRequest* request = [[NSMutableURLRequest alloc]init];
- [request setURL:[NSURL URLWithString:urlStr]]; //設置地址
- [request setHTTPMethod:@"POST"]; //設置發送方式
- [request setTimeoutInterval: 20]; //設置連接超時
- [request setValue:length forHTTPHeaderField:@"Content-Length"]; //設置數據長度
- [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //設置發送數據的格式
- [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; //設置預期接收數據的格式
- [request setHTTPBody:postData]; //設置編碼後的數據
- //發起連接,接受響應
- NSHTTPURLResponse* urlResponse = nil;
- NSError *error = [[NSError alloc] init] ;
- NSData *responseData = [NSURLConnection sendSynchronousRequest:request
- returningResponse:&urlResponse
- error:&error];
- NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; //返回數據,轉碼
- NSLog(responseString);
- NSLog(@"post_end");
- return responseString;
- }
將下面的代碼加入到某個類中,然後調用httpPost方法就可以了,這個是異步的
- -(void)httpPost:(NSString*)strcontext URL:(NSString*)urlstr{
- strcontext = nil;
- NSLog(@"url--%@",urlstr);
- NSLog(@"param--%@",strcontext);
- assert(strcontext!=NULL);
- assert(urlstr!=NULL);
- NSData* postData = [strcontext dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];//轉碼
- NSString* postLength = [NSString stringWithFormat:@"%d",[postData length]];
- NSMutableURLRequest* request = [[[NSMutableURLRequest alloc]init]autorelease];
- [request setURL:[NSURL URLWithString:urlstr]]; //設置地址
- [request setHTTPMethod:@"POST"]; //設置發送方式
- [request setTimeoutInterval: 20]; //設置連接超時
- [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; //設置數據長度
- [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //設置發送數據的格式
- [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; //設置預期接收數據的格式
- [request setHTTPBody:postData]; //設置編碼後的數據
- NSURLConnection*conn=[[NSURLConnection alloc]initWithRequest:request delegate:self]; //設置類代理,注意要是self哦
- if(conn)
- {
- NSLog(@"ConnectionSuccess");
- [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
- [conn retain];
- }
- else
- {
- NSLog(@"ConnectionFailed");
- //informtheuserthatthedownloadcouldnotbemade
- }
- }
- #pragma mark------------------以下爲相應的回調函數-------------------------------
- //收到響應時,會觸發
- -(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response{
- //注意這裏將NSURLResponse對象轉換成NSHTTPURLResponse對象才能去
- NSHTTPURLResponse*httpResponse=(NSHTTPURLResponse*)response;
- if([response respondsToSelector:@selector(allHeaderFields)]){
- NSDictionary*dictionary=[httpResponse allHeaderFields];
- NSLog(@"didReceiveResponse1:%@",[dictionary description]);
- NSLog(@"didReceiveResponse2:%d",[response statusCode]);
- }
- }
- //鏈接錯誤
- -(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error{
- //[selfperformSelectorOnMainThread:@selector(httpConnectEnd)withObject:nil waitUntilDone:NO];
- NSLog(@"didFailWithError:%@",[error localizedDescription]);
- }
- //Calledwhenachunkofdatahasbeendownloaded.
- //接收數據每收到一次數據,會調用一次
- -(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data{
- //Processthedownloadedchunkofdata.
- NSLog(@"didReceiveData_length:%d",[data length]);
- // NSLog(@"didReceiveData_data:%d",[data description]);
- [[self responseData] appendData:data];
- NSLog(@"didReceiveData_data%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
- //[selfperformSelectorOnMainThread:@selector(updateProgress)withObject:nil waitUntilDone:NO];
- }
- //接收結束
- -(void)connectionDidFinishLoading:(NSURLConnection*)connection{
- NSLog(@"connectionDidFinishLoading:%@",connection);
- //NSLog(@"%lld",received_);
- //[selfperformSelectorOnMainThread:@selector(httpConnectEnd)withObject:nil waitUntilDone:NO];
- //Settheconditionwhichendstherunloop.
- NSString *responseString = [[NSString alloc] initWithData:[self responseData] encoding:NSUTF8StringEncoding];
- [responseData release];
- NSLog(@"responseString:%@",responseString);
- }