所以我正在制作 android 应用的 iOS 版本。所有的网络服务已经设置但他们没有太多详细的文档,所以我选择从Android代码推断。项目完成除了一件小事 - HTTP Multipart请求。
我需要将java
中的网络请求实施到objective-c
。这样做是将图像和用户ID 一起上传到服务器并获取响应。以下是相关的Java代码:
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest=new HttpPost(Constant.server_url+"usermain/Profileimagesave");
String fileName = String.format("File_%d.png",new Date().getTime());
ByteArrayBody bab = new ByteArrayBody(data, fileName);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", bab);
reqEntity.addPart("WebID", new StringBody(SharedPreferenceSession.getValue(getApplicationContext(), "WebId")));
postRequest.setEntity(reqEntity);
我已经尝试将近一周时间将此代码模仿到objective-c
。这是我在参加stefan's answer后可以管理的内容:
[manager POST:urlstr parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
[formData appendPartWithFileData:imageData
name:@"blahblah.jpg"
fileName:@"blahblah.jpg"
mimeType:@"image/jpeg"];
[formData appendPartWithFormData:[webid dataUsingEncoding:NSUTF8StringEncoding]
name:@"WebID"];
}
progress:nil
success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"Response: %@", responseObject);
}
failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"%@",[error.userInfo objectForKey:JSONResponseSerializerWithDataKey]);
}];
我一直在尝试上述代码的很多变体,但由于我对多部分请求没有清楚的理解,我觉得我会绕圈子去。
我从服务器收到500错误。
PHP警告
copy(../ UserData / ThumbPhotos / MOB274565CCM_20151230062220。):无法打开流:没有这样的文件或目录
1773 $ login1 = $ login-&gt; LoginID;
1774 $ curr_date = date(&#34; YmdHis&#34;);
1775 $ currentname = $ webid。&#34; _&#34;。+ $ curr_date;
1776
1777 // echo&#34;文件大小:&#34;。$ _文件[&#39;文件&#39;] [&#39;尺寸&#39;];
1778 $ info = pathinfo($ _ FILES [&#39; file&#39;] [&#39; name&#39;]);
1779 $ ext = $ info [&#39; extension&#39;];
1780 $ filename =&#34; {$ currentname}。{$ ext}&#34 ;;
1781 $ sourcePath = $ _FILES [&#39; file&#39;] [&#39; tmp_name&#39;];
1782 $ targetPath =&#34; ../ UserData / ThumbPhotos /&#34;。$ filename;
1783 $ targetPath1 =&#34; ../ UserData / UserPhoto /&#34;。$ filename;
1784 move_uploaded_file($ sourcePath,$ targetPath);1785副本($ targetPath,$ targetPath1);
1786 $ userfiles = new UserFiles;
1787 $ userfiles-&gt; LoginID = $ login1;
1788 $ userfiles-&gt; FileActualName = $ filename;
1789 $ userfiles-&gt; FileTempName = $ filename;
1790 $ userfiles-&gt; ThumbImage = $ filename;
1791 $ userfiles-&gt; Type =&#34; IMAGE&#34 ;;
1792 $ userfiles-&gt; ProfileImage = 0;
1793 $ userfiles-&gt; deletedStatus = 0;
1794 $ userfiles-&gt; UploadBy = $ login1;
1795 $ userfiles-&gt; UploadDate = date(&#34; Y-m-d&#34;);
1796 $ userfiles-&gt; deletedby = NULL;
1797 $ userfiles-&gt; PasswordProtected = 0;
我无法理解上述代码中的内容,因为我不熟悉HTTP请求的功能。所以任何可以帮助我理解这一点的内容都是受欢迎的,可能是工作代码示例,调试请求的提示或这些内容如何工作的评论
或
android代码工作正常。那么有什么方法可以检查android和iOS应用程序上的传出请求,也许会尝试通过反复试验来匹配它们?
提前致谢
答案 0 :(得分:2)
我建议您使用AFNetworking。
在这种情况下,您想要上传一些图像,因此我将为您提供以下代码:
NSData *imageData = [NSData dataWithContentsOfFile:filename];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:@"files"
fileName:photoName mimeType:@"image/jpeg"];
[formData appendPartWithFormData:[key1 dataUsingEncoding:NSUTF8StringEncoding]
name:@"key1"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
答案 1 :(得分:1)
尝试使用NSMutableURLRequest上传任何图像
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:YOUR_URL]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:_YOUR_IMAGE_DATA]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"posttype\"\r\n\r\n%@", postType] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"postimgtext\"\r\n\r\n%@", postTextType] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n%@", USER_ID] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSError *error = nil;
NSURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"response : %@", response);
}