我遇到了上传问题,服务器在dotnet中我的iphone代码
-(NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
就像charam一样工作,但是当我尝试通过我的Android应用程序上传时,如果在url中有一些特殊字符(=),它总会从服务器返回错误。
见下面的代码
Bitmap icon = BitmapFactory.decodeResource(this.contextParam.getResources(),
ByteArrayOutputStream bao = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
String ba1 = Base64.encodeToString(ba, Base64.DEFAULT);
Log.e("Data:", ba1);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "dsfsdfsd"));
nameValuePairs.add(new BasicNameValuePair("filenamewithextension", "upload.png"));
nameValuePairs.add(new BasicNameValuePair("newfilename", "Testing upload"));
nameValuePairs.add(new BasicNameValuePair("entityid", "10"));
nameValuePairs.add(new BasicNameValuePair("filestream", ba1));
Log.i(TAG, httppost.getURI().toString());
Log.d(TAG, nameValuePairs.toString());
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
String base64EncodedCredentials = Base64.encodeToString(CREDENTIALS.getBytes(), Base64.NO_WRAP);
httppost.addHeader("Authorization", "Basic " + base64EncodedCredentials);
httppost.addHeader("Content-Type", "application/largedata");
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
return getStringFromInputStream(response.getEntity().getContent());
} catch (ClientProtocolException e) {
Log.e(TAG, e.getLocalizedMessage());
} catch (IOException e) {
Log.e(TAG, e.getLocalizedMessage());
}
return responseString;
上面的代码服务器总是返回“请求错误”。 任何人都可以建议它为什么会发生 旁注:我不能改变服务器实现,我必须在android端找到解决方案
答案 0 :(得分:0)
我不是百分百肯定,因为你没有发布你的日志,但是我在iOS上工作的应用程序遇到了类似的问题。对于HTTP,它们具有编码规则。您可能需要对您的HTTPPost进行“百分比转义编码”。
例如:
www.website.com/security/authorizekey?url=www.website.com?user=bob
当上述链接用作我的网址时,HTTP的规则会截断我的帖子,以便服务器只收到“www.website.com/security/authorizekey?url=www.website.com?user=”并且截断我的用户“bob”。
根据RFC 3986,应该允许未转义的字符是字母数字字符,加上“ - ”,“。”,“_”和“〜”:
2.3. Unreserved Characters
Characters that are allowed in a URI but do not have a reserved purpose are called unreserved. These include uppercase and lowercase letters, decimal digits, hyphen, period, underscore, and tilde.
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
有关详细信息,请参阅上述RFC 3986的第2部分。
无论如何,解决方案可能类似于以下内容:
潜在解决方案:
String query = URLEncoder.encode("apples oranges", "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;