gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:31:33 26 4
gpt4 key购买 nike

你好,我是 iOS 新手,需要向 php 服务器提交图像。为此我已将图像转换为 Base64 格式。并且需要将此base64字符串发送到PHP 服务器。我正在使用的代码是这样的,请帮助我在 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];
}

最佳答案

我这篇文章有 base 64 编码数据的答案

Base64 Encoding for NSString

从字符串获取 NSData

NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];

和来自数据的 NSString

NSString* str =[NSString stringWithUTF8String:[数据字节]];

直接来 self 的代码的这个片段确实很容易发布连接

-(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;
}

props -> 用于包装内容字符串和 url 的基本类

@interface ServiceProps : NSObject
@property () NSString* contentString;
@property () NSURL* connectionURL;
@end

delegate -> 一个接口(interface)类,它包含你的后期服务逻辑里面取决于你的服务

@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

关于ios - 如何使用 POST 方法将 base64 格式的字符串发送到 PHP 服务器,ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19703463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com