2012-05-08 32 views
0

我正在使用NSURLConnection和NSURLRequest來執行一些XML數據到服務器的HTTP POST。NSURLConnection/NSURLRequest默認將POST設置爲表單提交?

但是,服務器無法在HTTP正文中找到數據,因爲它已經被打包了,假設網頁正在進行表單提交(參數= form-urlencoded正在設置,大概默認情況下是NSURLConnection?)。

這不是我做的明確,我使用簡單地將身體的數據:

[request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]]; 

如何才能停止/覆蓋這個行爲?

回答

0

我不知道的默認值,但你可以改變它:

[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

看看到documentation

0

在這裏,我在Web服務器上發佈XML數據並使用NSURLConnection獲取xml響應。步驟如下:NSURLConnection的類的

1)發表的數據服務器通過NSURLConnection的異步請求

NSMutableString *productDetailString =[[NSMutableString alloc] init]; 

    [productDetailString appendString:@"<product>"]; 
    [productDetailString appendFormat:@"<product_name>%@</product_name>",name]; 
    [productDetailString appendString:@"<product_id>1024</product_id>"]; 
    [productDetailString appendString:@"</product>"];   


    NSString *message=[[NSString alloc] initWithFormat:@"_xmlrequestPostage=%@",productDetailString]; 


// NSString *message=[[NSString alloc] initWithFormat:@"_xmlrequestPostage="        
//      "<product>\n" 
//       "<product_name>%@</product_name>\n" 
//       "<product_id>%@</product_id>\n" 
//      "</product>\n",name, id]; 


    NSString *urlString = [NSString stringWithFormat:@"http://flightsflights.com.au/Mega/product.asmx/GetProductPrice"]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 


    //set headers 

    NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded"]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    [request addValue:[NSString stringWithFormat:@"%d",[message length]] forHTTPHeaderField:@"Content-Length"]; 

    //create the body 

    NSMutableData *postBody = [[NSMutableData alloc]initWithData:[[NSString stringWithFormat:@"%@",message] dataUsingEncoding:NSUTF8StringEncoding]]; 

    //post 
    [request setHTTPBody:postBody]; 

    //get response 
    NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    if(connection) 
    { 
     webData=[[NSMutableData data] retain]; 
    } 
    else 
    { 
     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Mega Australia" message:@"Problem in network." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 

2)通知代表

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
    { 
     [webData setLength: 0]; 
    } 
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
    { 
     [webData appendData:data]; 
    } 
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
    { 
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error" message:@"ERROR with conenction " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [alert show]; 
     [alert release]; 
    [connection release]; 
    [webData release]; 
    } 
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
     if(webData==nil) 
     { 
      UIAlertView *thealert=[[UIAlertView alloc] initWithTitle:@"Error" message:@"No data found." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
      [thealert show]; 
      [thealert release]; 
     } 
     else 
     { 

      NSString *str=[[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; 

      str=[str stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"]; 
      str=[str stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"]; 

      NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 
      product=[[NSMutableArray alloc] init]; 

      NSXMLParser *xmlParser=[[NSXMLParser alloc] initWithData:data]; 
      xmlParser.delegate=self; 
      [xmlParser parse]; 
      webData=nil; 
      [webData release]; 
     } 
    } 

3)添加下列變量在h文件: NSMutableData * webData; NSMutableDictionary * aProduct; NSMutableArray * product;

 NSMutableString *name, *pro_id, *currentElement, *statusCode; 
    } 
    @property (retain, nonatomic) NSMutableString *name; 
    @property (retain, nonatomic) NSMutableString *pro_id; 
    @property (retain, nonatomic) NSMutableString *statusCode; 

4)合成的名字,pro_id和中的StatusCode .m文件

5)通知代表們NSXMLParse類

 -(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
    { 
      currentElement=[elementName copy]; 

      if([elementName isEqualToString:@"product"]) 
      { 
       self.name=[[NSMutableString alloc] init]; 
       self.pro_id=[[NSMutableString alloc] init]; 

       aProduct=[[NSMutableDictionary alloc] init]; 
      } 
      else if([elementName isEqualToString:@"status"]) 
      { 
       self.statusCode=[[NSMutableString alloc] init]; 
      } 
    } 

    -(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
    { 
      if([currentElement isEqualToString:@"product_name"]) 
      { 
       [self.name appendString:string]; 
      } 
      else if([currentElement isEqualToString:@"product_id"]) 
      { 
       [self.pro_id appendString:string]; 
      } 
      else if([currentElement isEqualToString:@"status"]) 
      { 
       [self.statusCode appendString:string]; 
       if(![self.statusCode isEqualToString:@"200"]) 
       { 
         UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Mega Australia" message:@"Input string is not in a correct format." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
         [alert show]; 
         [alert release]; 
         return; 
       } 
      } 
     } 

     - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
     { 
      if ([elementName isEqualToString:@"product"]) 
      { 
       [aProduct setObject:self.name forKey:@"name"]; 
       [aProduct setObject:self.pro_id forKey:@"id"]; 

       [product addObject:[aProduct copy]]; 
      } 
     } 

     - (void)parserDidEndDocument:(NSXMLParser *)parser 
     { 
      return; 
     } 
+0

偉大的工作人士Himanshu –

相關問題