Java BasicNameValuePair字符转义
我试图将包含html实体以及商标和版权符号等奇数字符的字符串传入java中BasicNameValuePair的值。当我尝试执行它时,立即失败。代码:Java BasicNameValuePair字符转义,java,urlencode,Java,Urlencode,我试图将包含html实体以及商标和版权符号等奇数字符的字符串传入java中BasicNameValuePair的值。当我尝试执行它时,立即失败。代码: HttpPost httpPost = new HttpPost("http://localhost:8983"); String test = "<field name=\"description\">Example®<ul><li> This is a bunch of st
HttpPost httpPost = new HttpPost("http://localhost:8983");
String test = "<field name=\"description\">Example®<ul><li> This is a bunch of stuff I would like to post</li></ul></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();
}
HttpPost-HttpPost=新的HttpPost(“http://localhost:8983");
String test=“Example®ulli这是我想发布的一堆东西/li/ul
List-nvps=newarraylist();
添加(新的BasicNameValuePair(“stream.body”,test));//这是字符串
添加(新的BasicNameValuePair(“提交”、“真实”);
试一试{
setEntity(新的UrlEncodedFormEntity(nvps));
}捕获(不支持DencodingException ex){
例如printStackTrace();
}
HttpResponse响应;
试一试{
response=httpClient.execute(httpPost);
}捕获(IOEX异常){
例如printStackTrace();
}
我认为“&符号引起了一个问题,因为它们可能是URL必须单独处理的查询字符。如何在URL中执行这种类型的字符串?检查此链接以处理特殊字符。我想这可能会有帮助 放置在HTML标记中的任意文本通常需要更改,以确保生成的HTML保持有效