【问题标题】:how to send base64 formatted string to PHP server using POST method, ios如何使用 POST 方法将 base64 格式的字符串发送到 PHP 服务器,ios
【发布时间】:2013-10-31 10:04:39
【问题描述】:

您好,我是 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];
}

【问题讨论】:

    标签: ios http post base64 nsmutableurlrequest


    【解决方案1】:

    我这篇文章有base 64 encode a data的答案

    Base64 Encoding for NSString

    对于字符串中的 NSData

    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    

    和来自数据的 NSString

    NSString* str =[NSString stringWithUTF8String:[data bytes]];

    这个sn-p直接来自我的代码很容易发布连接

    -(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 -> 一个包含你的后期服务逻辑的接口类 里面取决于你的服务

    @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
    

    【讨论】:

    • 好吧,我在问关于向服务器发送数据的问题
    • doPostConnection:(ServiceProps*) props delegate:(id) 委托请解释这个参数
    猜你喜欢
    • 1970-01-01
    • 2014-07-19
    • 2019-05-20
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多