Ios 使用AF2上传图像和其他参数
我正在尝试将图像从我的应用程序发送到服务器。这是带有.net web服务的windows服务器 我试图调用的服务接收三个参数:一个数字标识符用户Id,用于告诉服务器哪个用户正在发送新图片;图像作为base64编码字符串;图像格式,始终为.png 我的上传功能是:Ios 使用AF2上传图像和其他参数,ios,xml,file-upload,base64,afnetworking-2,Ios,Xml,File Upload,Base64,Afnetworking 2,我正在尝试将图像从我的应用程序发送到服务器。这是带有.net web服务的windows服务器 我试图调用的服务接收三个参数:一个数字标识符用户Id,用于告诉服务器哪个用户正在发送新图片;图像作为base64编码字符串;图像格式,始终为.png 我的上传功能是: - (void)updateProfilePic:(UIImage *)profilePic forUser:(User *)user { //Encode the image in the requested format
- (void)updateProfilePic:(UIImage *)profilePic forUser:(User *)user
{
//Encode the image in the requested format
NSData *imageData = UIImagePNGRepresentation(profilePic);
NSString *encodedString = [imageData base64EncodedStringWithOptions: NSDataBase64Encoding64CharacterLineLength];
//Build the URL string
NSURL *url = [NSURL URLWithString:BaseURL];
NSString *soapBody = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>\n"
"<UpdateUserProfilePic_iOS xmlns=\"http://mycompany.com/webservices/>\n"
"<userId>%d</userId>\n"
"<imgbinStr>%@</imgbinStr>\n"
"<imgFormat>.png</imgFormat>\n"
"</UpdateUserProfilePic_iOS>\n"
"</soap:Body>\n"
"</soap:Envelope>",
(int)user.userId, encodedString];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapBody length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
[request addValue:@"\"http://mycompany.com/webservices/UpdateUserProfilePic_iOS\"" forHTTPHeaderField:@"SOAPAction"];
[request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
//parse NSXMLParser object here if request successful
if ([responseObject isKindOfClass:[NSXMLParser class]]) {
XMLParser *xml = [[XMLParser alloc] init];
NSString *responseString = [xml parseResponse:responseObject];
NSLog(@"Update picture response: %@", responseString);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"Error: %@", error);
}];
[operation start];
}
到目前为止,答复是错误的。但是,错误是400,这使我认为问题在于我格式化请求的方式:
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7999aa20 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x799858c0> { URL: http://myserverIP/UserProfileSvc.asmx } { status code: 400, headers {
"Cache-Control" = private;
"Content-Length" = 0;
"Content-Type" = "text/xml; charset=utf-8";
Date = "Thu, 16 Oct 2014 00:03:53 GMT";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }, NSErrorFailingURLKey=http://myserverIP/UserProfileSvc.asmx, NSLocalizedDescription=Request failed: bad request (400), com.alamofire.serialization.response.error.data=<>}
我认为问题不在服务器端。我可能格式化错误,但我不知道可能是什么。提前感谢hiii您找到解决方案了吗?我也有同样的问题