使用http POST请求将图像上传到服务器

时间:2014-02-23 14:04:07

标签: ios http blackberry httprequest multipartform-data

我正在尝试将图片上传到服务器但是没有正确上传获取http错误415当我尝试在黑莓上传它正确上传时,任何人都可以告诉我这里我错过的是BB和ios的代码< / p>

iOS代码

NSData *imageData = UIImageJPEGRepresentation(img, 90); /// convert image in NSData
NSString *urlString = @"http://api.Domain.com:8080/upImg/"; // your url
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data"];
 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString   stringWithFormat:@"pic"]dataUsingEncoding:NSUTF8StringEncoding ]];
[postBody appendData:[NSData dataWithData:imageData]];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPBody:postBody];

// now lets make the connection to the web
 NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
 NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

BB中的代码:

httpConnection = MakeUrl.returnUrl(url);
httpConnection.setRequestMethod(HttpConnection.POST);

 httpConnection.setRequestProperty("Content-Type",
  HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA);
httpConnection.setRequestProperty("User-Agent","Mozilla/5.0 (X11; U; Linux "+ "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
 httpConnection.setRequestProperty("Accept","text/html,application/xml,"+ "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/jpg,*/*;q=0.5");
 httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

  URLEncodedPostData _postData = new URLEncodedPostData("UTF-8",false);
 _postData.append("pic", postData);
OutputStream os = httpConnection.openOutputStream();
os.write(_postData.getBytes());
os.flush();

0 个答案:

没有答案