我正在尝试发布一个字符串并检索服务器的响应但由于某种原因它似乎不起作用,我过去使用了一些类似的代码,它运行良好... 这是我的代码,如果您发现错误或者您有建议,请告诉我
NSURL *url = [NSURL URLWithString:@"http://www.NAME.info/test/check.php"];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = @{
@"Content-Type" : @"application/json"
};
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";
NSError *error = nil;
NSString* str = @"username=test101";
NSData* jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
if (!er
ror) {
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:jsonData completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString* result2 = [responseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *secondString = [result2 stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"*%@*",secondString);
NSLog(@"RESULT RETURNED%@",response.debugDescription);
}];
[uploadTask resume];
}
这样做的结果是:
2015-03-26 19:35:03.305 ChannerApp[690:61802] *error_post*
2015-03-26 19:35:03.305 ChannerApp[690:61802] RESULT RETURNED<NSHTTPURLResponse: 0x17023a3a0> { URL: http://www.NAME.info/test/check.php } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Length" = 10;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Thu, 26 Mar 2015 17:35:35 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = "Apache/2.4.7 (Ubuntu)";
"X-Powered-By" = "PHP/5.5.9-1ubuntu4.6";
} }