BasicNameValuePair字符转义

时间:2013-07-26 17:45:08

标签: java urlencode

我正在尝试将包含html实体的字符串以及商标和版权符号等奇数字符传入java中的BasicNameValuePair的值。当我尝试执行它时,现在无声地失败。代码:

HttpPost httpPost = new HttpPost("http://localhost:8983");
String test = "<field name=\"description\">Example®&lt;ul&gt;&lt;li&gt; This is a bunch of stuff I would like to post&lt;/li&gt;&lt;/ul&gt;</field>
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("stream.body", test));   // HERE is the string
nvps.add(new BasicNameValuePair("commit","true"));

try {
    httpPost.setEntity(new UrlEncodedFormEntity(nvps));
} catch (UnsupportedEncodingException ex) {
    ex.printStackTrace();
}

HttpResponse response;
try {
    response = httpClient.execute(httpPost);
} catch (IOException ex) {
    ex.printStackTrace();
}

我认为“&amp;符号导致问题,因为它们可能是URL必须单独处理的查询字符。如何在URL中执行此类型的字符串?

1 个答案:

答案 0 :(得分:0)

检查此链接是否处理特殊字符。我认为这可能会有所帮助

“放置在HTML标记中的任意文本通常需要更改,以确保生成的HTML仍然有效。” http://www.javapractices.com/topic/TopicAction.do?Id=96