【问题标题】:Can't upload base64 string having special characters无法上传具有特殊字符的 base64 字符串
【发布时间】:2014-09-13 15:22:47
【问题描述】:

我遇到上传问题,服务器位于我的 iphone 代码所在的 dotnet 中

  -(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 端找到解决方案

【问题讨论】:

  • application/largedata 看起来像你编造的东西。 application/x-www-form-urlencoded 将是其他人用于此类内容的内容。否则服务器产生的错误,请在服务器端进行调试。

标签: java android string base64 special-characters


【解决方案1】:

我不能 100% 确定,因为您没有发布您的日志,但我在为 iOS 开发的应用程序中遇到了类似的问题。对于 HTTP,它们具有编码规则。您可能需要“百分比转义编码”您的 HTTPPost。

例如:

www.website.com/security/authorizekey?url=www.website.com?user=bob

当上面的链接被用作我的 URL Post 时,HTTP 的规则会截断我的 Post,以便服务器只收到 "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;

【讨论】:

  • 您要发布的网址是什么样的?
猜你喜欢
  • 2012-09-07
  • 2011-04-11
  • 2019-09-10
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
相关资源
最近更新 更多