iphone http通訊

//prepar request
    NSString *urlString = [NSString stringWithFormat:@"http://urlToSend.com"];
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    
        //set headers
    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    //create the body
    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
    
        //post
    [request setHTTPBody:postBody];
    
    //get response
    NSHTTPURLResponse* urlResponse = nil;  
    NSError *error = [[NSError alloc] init];  
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"Response Code: %d", [urlResponse statusCode]);
    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
        NSLog(@"Response: %@", result);
            
                //here you get the response

    }


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
 NSString *new = [searchBar.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
 NSString *urlString = [NSString stringWithFormat:@"http://192.168.1.104/test.php?cid=%@",new];
 NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
  NSString *strUrld8 = [urlString stringByAddingPercentEscapesUsingEncoding:enc];
 //調用http get請求方法 
 [self sendRequestByGet:strUrld8];
}
//HTTP get請求方法
- (void)sendRequestByGet:(NSString*)urlString
{  
 NSURL *url=[NSURL URLWithString:urlString];
 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url
                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
               timeoutInterval:60];
 //設置請求方式爲get
 [request setHTTPMethod:@"GET"];
 //添加用戶會話id
 [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
 //連接發送請求
 receivedData=[[NSMutableData alloc] initWithData:nil];
 NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
 [request release];      
 [conn release];
}
- (void)connection:(NSURLConnection *)aConn didReceiveResponse:(NSURLResponse *)response {
    // 注意這裏將NSURLResponse對象轉換成NSHTTPURLResponse對象才能去
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
    if ([response respondsToSelector:@selector(allHeaderFields)]) {
        NSDictionary *dictionary = [httpResponse allHeaderFields];
        NSLog(@"[email=dictionary=%@]dictionary=%@",[dictionary[/email] description]);
  
    }
}
//接收NSData數據
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data {
 [receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error{
 NSLog(@"[email=error=%@]error=%@",[error[/email] localizedDescription]);
}
//接收完畢,顯示結果
- (void)connectionDidFinishLoading:(NSURLConnection *)aConn {
   NSString *results = [[NSString alloc]
                         initWithBytes:[receivedData bytes]
                         length:[receivedData length]
                         encoding:NSUTF8StringEncoding];
 NSLog(@"results=%@",results);
}   


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章