iphone http通讯

来源:互联网 发布:淘宝查订单号查询 编辑:程序博客网 时间:2024/05/17 22:41
//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);}