• objectc NSMutableURLRequest模拟表单提交


    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

     
     
        [urlRequest setHTTPMethod:@"POST"];
     
        [urlRequest setValue: [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDRY]
     
          forHTTPHeaderField:@"Content-Type"];
     
     
     
     
    int len=512;
     
    if(imageData !=nil){
     
    len = imageData.length + 512;
     
    }
     
    NSMutableData  * postData =[NSMutableData dataWithCapacity:len];
     
     
    [postData  appendData: [[NSString  stringWithFormat:@"--%@\r\n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
     
    [postData  appendData: [[NSString  stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"token" ] dataUsingEncoding:NSUTF8StringEncoding]];
     
    [postData  appendData: [[NSString  stringWithFormat:@"%@",user.token] dataUsingEncoding:NSUTF8StringEncoding]];
     
    [postData  appendData: [[NSString  stringWithFormat:@"\r\n--%@\r\n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
     
     
     
    [postData  appendData: [[NSString  stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"timestamp" ] dataUsingEncoding:NSUTF8StringEncoding]];
     
    [postData  appendData:[[NSString   stringWithFormat:@"%@",timestamp] dataUsingEncoding:NSUTF8StringEncoding]];
     
    [postData  appendData:[[NSString   stringWithFormat:@"\r\n--%@\r\n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
     
     
    [postData  appendData: [[NSString  stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"signature" ] dataUsingEncoding:NSUTF8StringEncoding]];
     
    [postData  appendData:[[NSString   stringWithFormat:@"%@",signature] dataUsingEncoding:NSUTF8StringEncoding]];
     
     
     
    if (imageData != nil ) {
     
    [postData  appendData:[[NSString   stringWithFormat:@"\r\n--%@\r\n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
     
    [postData  appendData: [[NSString  stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";filename=\"temp.png\"\r\nContent-Type: image/png\r\n\r\n",@"upfile" ] dataUsingEncoding:NSUTF8StringEncoding]];
     
    [postData  appendData:imageData];
     
    }   
     
     
    [postData  appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
     
     
        [urlRequest setHTTPBody:postData];
        NSLog(@"%@",urlRequest); 
     
     
     
    NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];  
     
     
     
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];  
    NSLog(@"%@",returnString);  
     
  • 相关阅读:
    将表格图片转换成可编辑的表格数据
    找出比指定数大的数序
    导航窗格中无法显示的标题
    总表与分表相互更新(Excel代码集团)
    4行列转换(Power Query)
    用sumif取代vlookup
    提取指定文件夹中的文件名(Power Query)
    根据利润计算工资
    去掉字符串中最后一个字母
    buifo和os标准输入输出
  • 原文地址:https://www.cnblogs.com/zhwl/p/2837575.html
Copyright © 2020-2023  润新知