如何使用POST方法(ios)将base64格式的字符串发送到PHP服务器

如何使用POST方法(ios)将base64格式的字符串发送到PHP服务器,ios,http,post,base64,nsmutableurlrequest,Ios,Http,Post,Base64,Nsmutableurlrequest,您好,我是iOS新手,需要向php服务器提交一个图像。为此我 已将图像转换为base64格式。需要将此base64字符串发送到 PHP服务器。 代码我用的fot这是,请帮助我在ios中是完全新的 // Create your request string with parameter name as defined in PHP file NSString *myRequestString = [NSString stringWithFormat:@"comment=%@",@"test dat

您好,我是iOS新手,需要向php服务器提交一个图像。为此我 已将图像转换为base64格式。需要将此base64字符串发送到 PHP服务器。 代码我用的fot这是,请帮助我在ios中是完全新的

// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"comment=%@",@"test data"];
  // Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.sdevices.ru/flashrecorder/speechtotext.php"]];
// set Request Type
[request setHTTPMethod:@"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody:myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response); // here you get reasponse string

if ([response isEqualToString:@"Speech text!"]) {
    UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"Flash Recorder" message:@"Text is submitted!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alrt show];
}

我在这篇文章里有一个答案,是用base64编码一个数据

对于字符串中的NSData

NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
并从数据中提取字符串

NSString*str=[nsstringwithutf8string:[数据字节]

这段代码直接来自我的代码,可以轻松地进行后期连接

-(BOOL) doPostConnection:(ServiceProps*) props delegate:(id<URLConnectionDelegate>) delegate
{
     NSString *post = props.contentString;
     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
     NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:props.connectionURL];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Data-Type"];
    [request setHTTPBody:postData];
     NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:delegate];
     return conn != nil;
}
委托->包含售后服务逻辑的接口类 内部取决于您的服务

@protocol URLConnectionDelegate <NSObject>

- (id) initWithConnectionDelegate:(id<ConnectionDelegate>)delegate;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
@end
@协议URLConnectionLegate
-(id)initWithConnectionLegate:(id)委托人;
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据;
-(void)连接:(NSURLConnection*)连接失败错误:(NSError*)错误;
-(无效)连接IDFinishLoading:(NSURLConnection*)连接;
-(无效)连接:(NSURLConnection*)连接接收方响应:(NSURLConnection*)响应;
@结束

thiats ok,我正在询问向服务器发送数据的问题PostConnection:(ServiceProps*)props delegate:(id)delegate plase检查此参数
@protocol URLConnectionDelegate <NSObject>

- (id) initWithConnectionDelegate:(id<ConnectionDelegate>)delegate;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
@end