简体   繁体   English

使用UrlEncode在Apachi Http Client表单中正确编码百分号(%)

[英]Properly encoding percent sign (%) in Apachi Http Client form post using UrlEncode

This seems like an extremely easy problem but alas I cannot figure it out nor find a solution anywhere else. 这似乎是一个非常简单的问题,但是a,我无法解决,也无法在其他任何地方找到解决方案。 I'm concatenating a string that has a % within and for some reason it adds the number 25 after the %. 我正在串联一个包含%的字符串,由于某种原因,它在%之后添加了数字25。 Anyone know of a solution to this easy problem? 有人知道解决这个简单问题的方法吗?

String buttonCheck = "%26" + DATABASE.getValue("buttonCheck") + "%26";

Comes out to 出来

"%2526value%2526"

EDIT: It has become apparent that the issue is actually within URL encoding and I will add more relevant data to the issue. 编辑:很明显,问题实际上在URL编码内,我将向问题添加更多相关数据。

I am developing an Android App that parses HTML from a site and allows the user to interact with it through the Android UI. 我正在开发一个Android应用程序,该程序可以解析网站中的HTML,并允许用户通过Android UI与之交互。 I am having an issue with encoding % into a parameter for a form. 我在将%编码为表单的参数时遇到问题。

public class CLASS extends Activity
{
     DefaultHttpClient client = new DefaultHttpClient;
     String url = "http://www.url.com"
     HttpRequestBase method = new HttpPost(url);
     List<NameValuePair> nvps = new ArrayList<NameValuePair>();
     nvps.add("buttoncheck", "%26" + DATABASE.getValue("buttonCheck") + "%26");
     //DATABASE is simply a class that handles a HashMap

     HttpPost methodPost = (HttpPost) method;
     methodPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
     execute(method);
}

Rather than sending the form value of 而不是发送表单值

buttoncheck=%26value%26

I get 我明白了

buttoncheck=%2526value%2526

It won't come out as "%2526value%2526" in buttonCheck . buttonCheck ,它将不会显示为“%2526value%2526”。 I strongly suspect you're looking at a value later on - for example, after URI encoding. 我强烈怀疑您稍后会查看一个值-例如,在URI编码之后。 Work out what's doing that encoding, and what you actually want to be encoded. 找出正在执行的编码方式以及您实际要编码的内容。

Just to be clear, the 25 is completely distinct from the 26. You'll see the same thing if you get rid of the 26 completely, with 需要明确的是,25与26是完全不同的。如果完全摆脱26,您会看到相同的事情,

String buttonCheck = "%" + DATABASE.getValue("buttonCheck") + "%";

At that point I suspect you'll get 那时我怀疑你会得到

%25value%25

Basically something is just encoding the % as %25. 基本上,只是将%编码为%25。 For example, this would do it: 例如,这样做:

import java.net.*;

public class Test {
    public static void main(String[] args) throws Exception {
        String input = "%value%";
        String encoded = URLEncoder.encode(input, "utf-8");
        System.out.println(encoded); // Prints %25value%25
    }
}

Based on the answer to this question, I would guess that the literal % is being encoded to %25 in your string. 根据对这个问题的回答,我猜想您的字符串中的文字%已被编码为%25 Which explains the added 25 . 其中解释了添加的25 Without seeing relevant code, we won't be able to know why it gets there in the first place. 如果没有看到相关的代码,我们将无法知道为什么它首先到达那里。

apparently the string went through "percent-encoding" when it becomes part of a URI. 当字符串成为URI的一部分时,显然经过了“百分比编码”。

If that's the case, you should not do percent-encoding so early. 如果是这种情况,您不应该提早进行百分比编码。 instead 代替

String buttonCheck = "&" + DATABASE.getValue("buttonCheck") + "&";

which will end up in the URI as 将以URI结尾

"%26value%26"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM