简体   繁体   中英

Why does my NSMutableURLRequest fail?

I'm trying to get data from the server but I get error message that {"Message":"Wrong Process.","Success":false,"Cookie":null} from the server while I run the same link in the server test client and I get the right result. So please where would be my issue in my code please?

The Key is data

The value is <r_PM act=\\"par\\"/>

- (void)viewDidLoad {

    [super viewDidLoad];

    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/service/par.ashx"]];

    NSString *message = [[NSString alloc] initWithFormat:@"data=<r_PM act=\"par\"/>"];

    NSString *msgLength = [NSString stringWithFormat:@"%lu",(unsigned long)[message length]];

    [postRequest addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [postRequest addValue:msgLength                         forHTTPHeaderField:@"Content-Length"];

    [postRequest setHTTPMethod:@"POST"];
    [postRequest setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection * connection = [[NSURLConnection alloc]
                                    initWithRequest:postRequest
                                    delegate:self startImmediately:NO];

    [connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                          forMode:NSDefaultRunLoopMode];
    [connection start];

    NSURLResponse *response;
    NSError *err;

    responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&err];

    NSLog(@"responseData is: %@", responseData);

    NSString* newStr = [[NSString alloc] initWithData:responseData encoding: ];

    NSLog(@"Results : %@", newStr);

}

You are creating a request whose content-type is XML, but whose request body is not. The "data=..." format is not XML. In the absence of API documentation or sample code that generates a valid request, it's going to be hard for us to help you.

As an aside, your Objective-C code is issuing the request twice, one using the delegate-based NSURLConnection (for which you haven't shared your delegate implementation) and then again with sendSynchronousRequest . Only issues this once. And if you like the simplicity of the sendSynchronousRequest method, I'd suggest you use its sendAsynchronousRequest sibling, which is also pretty convenient, but isn't synchronous.


Offline, by examining request you were sending from another tool, we confirmed that the issue was that

  • Request is application/x-www-form-urlencoded not application/xml

    Thus, replace Content-Type line with:

     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
  • The data has to be percent escaped, eg:

     NSString *value = @"<r_PM act=\\"par\\"/>"; NSString *message = [NSString stringWithFormat:@"data=%@", [self percentEscapeString:value]]; 

    where, the percent escaping routine might be something like:

     - (NSString *)percentEscapeString:(NSString *)string { NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, (CFStringRef)@" ", (CFStringRef)@":/?@!$&'()*+,;=", kCFStringEncodingUTF8)); return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"]; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM